SlideShare a Scribd company logo
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
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
Interface
InterfaceInterface
Interface
vvpadhu
 
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
rani 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
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Abishek Purushothaman
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp 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 implementation
HoneyChintal
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Interface
InterfaceInterface
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
KumarUtsav24
 
Bai giang-uml-11feb14
Bai giang-uml-11feb14Bai 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
manish kumar
 
Java interface
Java interface Java interface
Java interface
HoneyChintal
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
TharuniDiddekunta
 
Opps
OppsOpps
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptxabstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
haiderkhooradnan
 

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
 
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptxabstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
 

Recently uploaded

Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
shahdabdulbaset
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
amsjournal
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
shivani5543
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 

Recently uploaded (20)

Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
UNLOCKING HEALTHCARE 4.0: NAVIGATING CRITICAL SUCCESS FACTORS FOR EFFECTIVE I...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 

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"); } }