SlideShare a Scribd company logo
1 of 26
Programming in Java
Abstract class and Interface
Contents
 Abstract Class
 Interface
 Object Class
Abstraction in Java
• Abstraction is a process of hiding the implementation
details and showing only functionality to the user.
• Another way, it shows only essential things to the user and
hides the internal details,
There are two ways to achieve abstraction in java
 Abstract class (0 to 100%)
 Interface (100%)
Abstract Class
• An abstract class is a class that is declared abstract.
• An abstract method is a method that is declared without an
implementation (without braces, and followed by a semicolon),
like this:
• Abstract class may or may not include abstract methods.
• Abstract classes cannot be instantiated, but they can be sub-
classed.
• If a class includes abstract methods, the class itself must be
declared abstract, as in:
public abstract class GraphicObject
{ // declare fields
// declare non-abstract methods
abstract void draw();
}
• An abstract class must be declared with an
abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods
also.
• It can have final methods which will force the
subclass not to change the body of the method.
Points to Remember
A method which is declared as abstract and does not have
implementation is known as an abstract method.
abstract void printStatus();
//no method body and abstract
Abstract Method in Java
abstract class Bank {
abstract int getRateOfInterest();
}
class SBI extends Bank {
int getRateOfInterest() {
return 7;
} }
class PNB extends Bank {
int getRateOfInterest() {
return 8;
} }
class TestBank {
public static void main(String args[]) {
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
} }
Interfaces
Interfaces
• Using the keyword interface, you can fully abstract a
class’ interface from its implementation.
• An interface is a collection of abstract methods.
• An interface is not a class.
• A class describes the attributes and behaviors of an object
whereas an interface contains behaviors that a class
implements.
• A class implements an interface, thereby inheriting the
abstract methods of the interface.
• Unless the class that implements the interface is abstract,
all the methods of the interface need to be defined in the
class.
Properties of Interfaces
 The interface keyword is used to declare an interface.
 Interfaces have the following properties:
 An interface is implicitly abstract. We do not need to use the
abstract keyword when declaring an interface.
 Each method in an interface is also implicitly public and
abstract, so the abstract keyword is not needed.
 Each variable in an interface is implicitly public, static and final.
 By interface, we can support the functionality of multiple
inheritance
 It can be used to achieve loose coupling
Interface Vs Class
An interface is similar to a class in the following ways:
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the
name of the interface matching the name of the file.
• The byte-code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding byte-
code file must be in a directory structure that matches the
package name.
Interface Vs Class
An interface is different from a class in several ways,
including:
• We cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface is not extended by a class; it is implemented by a
class.
• An interface can extend multiple interfaces.
Implementing Interfaces
 When a class implements an interface, then it has to
perform the specific behaviors of the interface.
 If a class does not perform all the behaviors of the
interface, the class must declare itself as abstract.
 A class uses the implements keyword to implement an
interface.
 The implements keyword appears in the class declaration
following the extends portion of the declaration.
General form of an interface:
interface Bank
{
float rateOfInterest();
}
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
//...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
Implementing Interfaces
• Once an interface has been defined, one or more classes can implement that
interface.
• To implement an interface, include the implements clause in a class definition,
and then create the methods required by the interface.
• The general form of a class that includes the implements clause looks like this:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
• If a class implements more than one interface, the interfaces are separated with a
comma.
• If a class implements two interfaces that declare the same method, then the same
method will be used by clients of either interface. The methods that implement an
interface must be declared public.
• Also, the type signature of the implementing method must match exactly the type
signature specified in the interface definition.
Example
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest()
{return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest()
{return 9.7f;}
}
class TestInterface{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
import java.io.*;
interface In1
{
final int a = 10;
void display(); // default void display(); }
class TestClass implements In1
{
public void display()
{
System.out.println("LPU");
}
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
import java.io.*;
interface In1
{
//static methods in interfaces which can be called independently
// without an object
static void display()
{
System.out.println("hello");
}
}
class TestClass implements In1
{
public void display()
{
System.out.println("LPU");
}
public static void main (String[] args)
{
In1.display();
}
}
Abstract Class Vs Interfaces
Abstract Class Interface
May contain non-abstract methods
and non-static non final data members
Contains only method declaration and
static final data members
Multiple Inheritance is not supported
through classes
Multiple Inheritance through
Interfaces is supported
Classes provide static classing
environment
Interfaces provide dynamic classing
environment
Inheritance using ‘extends’ keyword Inheritance using ‘implements’
keyword
The relationship between classes and interfaces
Object Class
 There is one special class, called Object, defined by Java.
 All other classes are subclasses of Object.
 A reference variable of type Object can refer to an object of any other
class.
 Arrays are implemented as classes, so a variable of type Object can also
refer to any array.
 In Object class, getClass( ), notify( ), notifyAll( ), and wait( ) are
declared as final.
Methods in Object class
What is the output of this program?
class Test
{
public static void main(String[] args)
{
Object obj = new String(“LPU in Punjab");
Class c = obj.getClass();
System.out.println(c.getName());
}
}
a. java.util.String
b. LPU in Punjab
c. java.lang.Object
d. java.lang.String
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.hashCode());
t = null;
// calling garbage collector
System.gc();
System.out.println("end");
}
protected void finalize()
{
System.out.println("finalize method called");
}
}
12.2 Abstract class and Interface.ppt

More Related Content

Similar to 12.2 Abstract class and Interface.ppt

8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#Sireesh K
 
Interface
InterfaceInterface
Interfacevvpadhu
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxBruceLee275640
 

Similar to 12.2 Abstract class and Interface.ppt (20)

8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Interface
InterfaceInterface
Interface
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Interface
InterfaceInterface
Interface
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
 
Bai giang-uml-11feb14
Bai giang-uml-11feb14Bai giang-uml-11feb14
Bai giang-uml-11feb14
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Java interface
Java interface Java interface
Java interface
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Opps
OppsOpps
Opps
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 

Recently uploaded

EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 

Recently uploaded (20)

POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

12.2 Abstract class and Interface.ppt

  • 1. Programming in Java Abstract class and Interface
  • 2. Contents  Abstract Class  Interface  Object Class
  • 3. Abstraction in Java • Abstraction is a process of hiding the implementation details and showing only functionality to the user. • Another way, it shows only essential things to the user and hides the internal details, There are two ways to achieve abstraction in java  Abstract class (0 to 100%)  Interface (100%)
  • 4. Abstract Class • An abstract class is a class that is declared abstract. • An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: • Abstract class may or may not include abstract methods. • Abstract classes cannot be instantiated, but they can be sub- classed.
  • 5. • If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
  • 6. • An abstract class must be declared with an abstract keyword. • It can have abstract and non-abstract methods. • It cannot be instantiated. • It can have constructors and static methods also. • It can have final methods which will force the subclass not to change the body of the method. Points to Remember
  • 7. A method which is declared as abstract and does not have implementation is known as an abstract method. abstract void printStatus(); //no method body and abstract Abstract Method in Java
  • 8. abstract class Bank { abstract int getRateOfInterest(); } class SBI extends Bank { int getRateOfInterest() { return 7; } } class PNB extends Bank { int getRateOfInterest() { return 8; } } class TestBank { public static void main(String args[]) { Bank b; b=new SBI(); System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); b=new PNB(); System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); } }
  • 10. Interfaces • Using the keyword interface, you can fully abstract a class’ interface from its implementation. • An interface is a collection of abstract methods. • An interface is not a class. • A class describes the attributes and behaviors of an object whereas an interface contains behaviors that a class implements. • A class implements an interface, thereby inheriting the abstract methods of the interface. • Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
  • 11. Properties of Interfaces  The interface keyword is used to declare an interface.  Interfaces have the following properties:  An interface is implicitly abstract. We do not need to use the abstract keyword when declaring an interface.  Each method in an interface is also implicitly public and abstract, so the abstract keyword is not needed.  Each variable in an interface is implicitly public, static and final.  By interface, we can support the functionality of multiple inheritance  It can be used to achieve loose coupling
  • 12. Interface Vs Class An interface is similar to a class in the following ways: • An interface can contain any number of methods. • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file. • The byte-code of an interface appears in a .class file. • Interfaces appear in packages, and their corresponding byte- code file must be in a directory structure that matches the package name.
  • 13. Interface Vs Class An interface is different from a class in several ways, including: • We cannot instantiate an interface. • An interface does not contain any constructors. • All of the methods in an interface are abstract. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces.
  • 14. Implementing Interfaces  When a class implements an interface, then it has to perform the specific behaviors of the interface.  If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.  A class uses the implements keyword to implement an interface.  The implements keyword appears in the class declaration following the extends portion of the declaration.
  • 15. General form of an interface: interface Bank { float rateOfInterest(); } access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; //... return-type method-nameN(parameter-list); type final-varnameN = value; }
  • 16. Implementing Interfaces • Once an interface has been defined, one or more classes can implement that interface. • To implement an interface, include the implements clause in a class definition, and then create the methods required by the interface. • The general form of a class that includes the implements clause looks like this: class classname [extends superclass] [implements interface [,interface...]] { // class-body } • If a class implements more than one interface, the interfaces are separated with a comma. • If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface. The methods that implement an interface must be declared public. • Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition.
  • 17. Example interface Bank{ float rateOfInterest(); } class SBI implements Bank{ public float rateOfInterest() {return 9.15f;} } class PNB implements Bank{ public float rateOfInterest() {return 9.7f;} } class TestInterface{ public static void main(String[] args){ Bank b=new SBI(); System.out.println("ROI: "+b.rateOfInterest()); }}
  • 18. import java.io.*; interface In1 { final int a = 10; void display(); // default void display(); } class TestClass implements In1 { public void display() { System.out.println("LPU"); } public static void main (String[] args) { TestClass t = new TestClass(); t.display(); System.out.println(a); } }
  • 19. import java.io.*; interface In1 { //static methods in interfaces which can be called independently // without an object static void display() { System.out.println("hello"); } } class TestClass implements In1 { public void display() { System.out.println("LPU"); } public static void main (String[] args) { In1.display(); } }
  • 20. Abstract Class Vs Interfaces Abstract Class Interface May contain non-abstract methods and non-static non final data members Contains only method declaration and static final data members Multiple Inheritance is not supported through classes Multiple Inheritance through Interfaces is supported Classes provide static classing environment Interfaces provide dynamic classing environment Inheritance using ‘extends’ keyword Inheritance using ‘implements’ keyword
  • 21. The relationship between classes and interfaces
  • 22. Object Class  There is one special class, called Object, defined by Java.  All other classes are subclasses of Object.  A reference variable of type Object can refer to an object of any other class.  Arrays are implemented as classes, so a variable of type Object can also refer to any array.  In Object class, getClass( ), notify( ), notifyAll( ), and wait( ) are declared as final.
  • 24. What is the output of this program? class Test { public static void main(String[] args) { Object obj = new String(“LPU in Punjab"); Class c = obj.getClass(); System.out.println(c.getName()); } } a. java.util.String b. LPU in Punjab c. java.lang.Object d. java.lang.String
  • 25. public class Test { public static void main(String[] args) { Test t = new Test(); System.out.println(t.hashCode()); t = null; // calling garbage collector System.gc(); System.out.println("end"); } protected void finalize() { System.out.println("finalize method called"); } }