Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Abstract Class & Interfaces

Abstract class in Java


 A class that is declared with abstract keyword, is known as
abstract class in java.
 It can have abstract and non-abstract methods (method with
body).
 It needs to be extended and its method implemented.
 It cannot be instantiated.
 It can have data member, abstract method, method body,
constructor and even main() method.
 abstract class A{}
Abstract method
 A method that is declared as abstract and does not have
implementation is known as abstract method.
 abstract void printStatus();
Example
abstract class A class abs_demo
{ {
abstract void disp(); public static void main(String args[]){
} B ob=new B();
ob.disp();
class B extends A { }
void disp() { }
OUTPUT
System.out.println("Hello");
Hello
}
}
Example
abstract class A class abs_demo
{ {
abstract void disp(); public static void main(String args[])
} {
B ob=new B();
class B extends A ob.disp();
{ }
void disp(){ } OUTPUT
System.out.println("Hello");
super.disp(); abstract method disp() in A
cannot be accessed directly
}
}
Example
abstract class A class abs_demo
{ {
void disp(){} public static void main(String args[]){
} B ob=new B();
ob.disp();
class B extends A }
{ }
void disp(){ OUTPUT
System.out.println("Hello");
Hello
}
}
Example
class A class abs_demo
{ {
abstract void disp(); public static void main(String args[])
} {
B ob=new B();
class B extends A ob.disp();
{ }
OUTPUT
void disp(){ }
System.out.println("Hello"); A is not abstract and does not
override abstract method disp() in A
}
} If there is any abstract method in a
class, that class must be abstract.
Example
abstract class A class abs_demo
{ {
abstract void disp(); public static void main(String args[])
} {
B ob=new B();
class B extends A ob.disp1();
{ } OUTPUT
void disp1(){ }
B is not abstract and does not override abstract
System.out.println("Hello"); method disp() in A
}
If you are extending any abstract class that have
} abstract method, you must either provide the
implementation of the method or make this class
abstract.
Example
abstract class A class C extends B
{ {
abstract void disp(); void disp()
} {
System.out.println("Hello in C");
abstract class B extends A }
{ }
void disp1(){ class abs_demo
System.out.println("Hello in B"); {
} public static void main(String args[])
} {
OUTPUT C ob=new C();
ob.disp1();
Hello in B }
}
Example
abstract class A class C extends B
{ {
abstract void disp(); void disp()
} {
System.out.println("Hello in C");
abstract class B extends A }
{ }
void disp1(){ class abs_demo
System.out.println("Hello in B"); {
} public static void main(String args[])
} {
OUTPUT C ob=new C();
ob.disp1();
Hello in B }
}
Example
abstract class A class C extends B
{ {
abstract void disp(); void disp()
} {
System.out.println("Hello in C");
abstract class B extends A }
{ }
void disp1(){ class abs_demo
System.out.println("Hello in B"); {
} public static void main(String args[])
} {
OUTPUT C ob=new C();
ob.disp();
Hello in C }
}
Interfaces
Introduction
 An interface in java is a blueprint of a class.
 It has static constants and abstract methods.
 The interface in java is a mechanism to achieve abstraction.
 There can be only abstract methods in the java interface not method body.
 It cannot be instantiated just like abstract class.
 The java compiler adds public and abstract keywords before the interface
method.
 The java compiler adds public, static and final keywords before data
members.
 Interface fields are public, static and final by default, and methods are
public and abstract.
Understanding relationship between classes and interfaces
Multiple inheritance in Java by interface
Interface & Abstract Class
interface Printable{ public static void main(String args[]){
void print(); TestInterface3 obj = new TestInterface3();
} obj.print();
interface Showable{ }
void print(); }
} OUTPUT

Hello
class TestInterface3 implements Printable,
Showable{ Printable and Showable interface have
same methods but its implementation is
public void provided by class TestTnterface1, so there
print(){System.out.println("Hello");} is no ambiguity.
Question
 Multiple inheritance is not supported through class in java but it is
possible by interface, why?

 Multiple inheritance is not supported in case of class because of


ambiguity.
 But it is supported in case of interface because there is no ambiguity as
implementation is provided by the implementation class.
Abstract class Interface
1) Abstract class can have abstract and Interface can have only abstract methods,
non-abstract methods. default and static methods also.
2) Abstract class doesn't support multiple
Interface supports multiple inheritance.
inheritance.
3) Abstract class can have final, non-final,
Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Multiple Inheritance
class student class student1 extends student
{ {
int rollno; String name; int marks1, marks2;
public void get(int x, String s){ public void get1(int x,int y){
rollno=x; marks1=x;
marks2=y;
name=s;
}
}
public void put1(){
public void put(){
System.out.println("Marks1="+marks1);
System.out.println("Name ="+name);
System.out.println("Marks2="+marks2);
System.out.println("Rollno ="+rollno); }}
}}
Multiple Inheritance
interface sports class result extends student1 implements
{ sports{
int total;
int sports=10;
public void get3() {
default public void put2()
get(111,“Manish");
{
get1(23,24);
System.out.println("Sports
}
marks="+sports);
public void cal()
}
{
}
total=marks1+marks2+sports;
put(); put1(); put2();
System.out.println("Total="+total); } }
Multiple Inheritance
class abs_demo OUTPUT
{
public static void main(String args[])  Name =Manish
 Rollno =111
{
 Marks1=23
result ob=new result();
 Marks2=24
ob.get3();
 Sports marks=10
ob.cal();
 Total=57
}
}
Interface & Abstract Class
interface A{ class abs_demo{
void a(); void b(); public static void main(String args[]){
void c(); void d(); A a=new M();
} a.a();
abstract class B implements A{ a.b();
public void c(){System.out.println("I am a.c();
C");} a.d();
} }}
OUTPUT
class M extends B{
public void a(){System.out.println("I am a");} I am in a
I am in b
public void b(){System.out.println("I am b");}
I am in C
public void d(){System.out.println("I am d");} I am in D
}
Example
interface X class XX
{ {
int x=10; public static void main(String args[])
} {
XXX ob=new XXX();
class XXX implements X System.out.println(ob.x);
{ }
int x=5; } OUTPUT
}
5
Example
interface X class XX
{ {
int x=10; public static void main(String args[])
} {
XXX ob=new XXX();
class XXX implements X System.out.println(X.x);
{ }
int x=5; } OUTPUT
}
10

You might also like