SD Abstract Class & Interface PDF
SD Abstract Class & Interface PDF
Dr Sujit Das
NIT Warangal
Abstraction
• It 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
• For example, sending SMS where you type the text and send the message.
You don't know the internal processing about the message delivery.
• Abstraction lets you focus on what the object does instead of how it does
it.
• Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
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()+" %");
}}
Abstract class having constructor, data member and methods
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 TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Multiple inheritance in Java by interface
Multiple inheritance in Java by interface
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
interface Printable{
void print();
}
interface Showable{
void print();
}
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and Interface has only static and final variables.
non-static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of abstract
interface. class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and An interface can extend another Java interface only.
implement multiple Java interfaces.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
Example of abstract class and interface in Java
//Creating interface that has 4 methods //Creating subclass of abstract class, now we need to pro
interface A{ vide the implementation of rest of the methods
void a();//bydefault, public and abstract class M extends B{
void b();
void c();
public void a(){System.out.println("I am a");}
void d(); public void b(){System.out.println("I am b");}
} public void d(){System.out.println("I am d");}
}
//Creating abstract class that provides t
he implementation of one method of A interface //Creating a test class that calls the methods of A interfa
abstract class B implements A{ ce
public void c(){System.out.println("I am C");}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}