In this article, we will discuss the difference between Abstract Class and Interface in Java with examples. I have covered the abstract class and interface in separate tutorials of OOPs Concepts so I would recommend you to read them first, before going though the differences.
1. Abstract class in java
2. Interface in Java
Abstract Class | Interface | |
1 | An abstract class can extend only one class or one abstract class at a time | An interface can extend any number of interfaces at a time |
2 | An abstract class can extend another concrete (regular) class or abstract class | An interface can only extend another interface |
3 | An abstract class can have both abstract and concrete methods | An interface can have only abstract methods |
4 | In abstract class keyword “abstract” is mandatory to declare a method as an abstract | In an interface keyword “abstract” is optional to declare a method as an abstract |
5 | An abstract class can have protected and public abstract methods | An interface can have only have public abstract methods |
6 | An abstract class can have static, final or static final variable with any access specifier | interface can only have public static final (constant) variable |
Each of the above mentioned points are explained with an example below:
Abstract class vs interface in Java
Difference No.1: Abstract class can extend only one class or one abstract class at a time
class Example1{ public void display1(){ System.out.println("display1 method"); } } abstract class Example2{ public void display2(){ System.out.println("display2 method"); } } abstract class Example3 extends Example1{ abstract void display3(); } class Example4 extends Example3{ public void display3(){ System.out.println("display3 method"); } } class Demo{ public static void main(String args[]){ Example4 obj=new Example4(); obj.display3(); } }
Output:
display3 method
Interface can extend any number of interfaces at a time
//first interface interface Example1{ public void display1(); } //second interface interface Example2 { public void display2(); } //This interface is extending both the above interfaces interface Example3 extends Example1,Example2{ } class Example4 implements Example3{ public void display1(){ System.out.println("display2 method"); } public void display2(){ System.out.println("display3 method"); } } class Demo{ public static void main(String args[]){ Example4 obj=new Example4(); obj.display1(); } }
Output:
display2 method
Difference No.2: Abstract class can be extended(inherited) by a class or an abstract class
class Example1{ public void display1(){ System.out.println("display1 method"); } } abstract class Example2{ public void display2(){ System.out.println("display2 method"); } } abstract class Example3 extends Example2{ abstract void display3(); } class Example4 extends Example3{ public void display2(){ System.out.println("Example4-display2 method"); } public void display3(){ System.out.println("display3 method"); } } class Demo{ public static void main(String args[]){ Example4 obj=new Example4(); obj.display2(); } }
Output:
Example4-display2 method
Interfaces can be extended only by interfaces. Classes has to implement them instead of extend
interface Example1{ public void display1(); } interface Example2 extends Example1{ } class Example3 implements Example2{ public void display1(){ System.out.println("display1 method"); } } class Demo{ public static void main(String args[]){ Example3 obj=new Example3(); obj.display1(); } }
Output:
display1 method
Difference No.3: Abstract class can have both abstract and concrete methods
abstract class Example1 { abstract void display1(); public void display2(){ System.out.println("display2 method"); } } class Example2 extends Example1{ public void display1(){ System.out.println("display1 method"); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
Interface can only have abstract methods, they cannot have concrete methods
interface Example1{ public abstract void display1(); } class Example2 implements Example1{ public void display1(){ System.out.println("display1 method"); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
Output:
display1 method
Difference No.4: In abstract class, the keyword ‘abstract’ is mandatory to declare a method as an abstract
abstract class Example1{ public abstract void display1(); } class Example2 extends Example1{ public void display1(){ System.out.println("display1 method"); } public void display2(){ System.out.println("display2 method"); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
In interfaces, the keyword ‘abstract’ is optional to declare a method as an abstract because all the methods are abstract by default
interface Example1{ public void display1(); } class Example2 implements Example1{ public void display1(){ System.out.println("display1 method"); } public void display2(){ System.out.println("display2 method"); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
Difference No.5: Abstract class can have protected and public abstract methods
abstract class Example1{ protected abstract void display1(); public abstract void display2(); public abstract void display3(); } class Example2 extends Example1{ public void display1(){ System.out.println("display1 method"); } public void display2(){ System.out.println("display2 method"); } public void display3(){ System.out.println("display3 method"); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
Interface can have only public abstract methods
interface Example1{ void display1(); } class Example2 implements Example1{ public void display1(){ System.out.println("display1 method"); } public void display2(){ System.out.println("display2 method"); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
Difference No.6: Abstract class can have static, final or static final variables with any access specifier
abstract class Example1{ private int numOne=10; protected final int numTwo=20; public static final int numThree=500; public void display1(){ System.out.println("Num1="+numOne); } } class Example2 extends Example1{ public void display2(){ System.out.println("Num2="+numTwo); System.out.println("Num2="+numThree); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); obj.display2(); } }
Interface can have only public static final (constant) variable
interface Example1{ int numOne=10; } class Example2 implements Example1{ public void display1(){ System.out.println("Num1="+numOne); } } class Demo{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }
Pari says
I love your site. all the java tutorial is excellent and easy to understand , Good work Keep going
saravana says
Thank so much,i like all the tutorials keep rocking……..
kanchana says
This website is really nice..i can easily understand the basic concept of java. thank u…..
Utkal says
Could you please provide one synario where we can go for abstract class
and we ll not go for it Interface
Ivkaran Singh says
Your website has done an excellent job explaining the Java features. Really easy to understand and follow, helping me a lot while preparing for interviews.
jocie says
Really superb explanation… Very good material to use in class room
satyajit says
please explain abstraction with a simple example
BHUSHAN says
Awsome site!!
Rupesh says
Easy to understand. nice example short and sweet. Keep going..
Divyanshu Ranjan says
Love your site.. Too much easy and understandable language.. With short and very easy example..
Manish says
Hello All,
Interface can have static method as well as method with default keyword
deepali says
Can abstract class implements interface?
Tonoy says
Awesome site for learning, very much organized and nicely presented. Keep it up. cheers!
Nadia khan says
superb material and easy to understand …
chandramohan says
i have a one doubt in long time….that was why we are using a interface in java…what is the main reason…all the interface example programs are i will tried at same time i remove the interface in that program when the program will be run so fully confused…tell me please clearly..what is the main reason for using interface in java
Xiu Chen says
Really like to read your website. Clear and easy~ thank your very much!!! ^_^
Kapil says
Can u pls explain with example when to use Abstract Class and Interface.
Trilok SIngh says
What is the main advantage of abstract class.. why we use this …
Zia ur Rehman says
Awesome. Excellent … Nice and precious Tutorial.
Good job! keep it up.
Luvnish Monga says
This tutorial is very good. Great Job (Y) It clears all the concepts of Abstract Classes as well of Interface.
Geetha says
superb site, easy to understand…..thank u….keep rocking!! :)