Difference Between Abstract Class And Interface In Java

Introduction

 
In this article, we will learn about Abstract class and Interface in the Java programming language. Abstract class and interface are used for abstraction. Both Abstract class and the interface can not be initiated.
 

Abstract class

 
In Java, when we use abstract keyword with any class, this class is called an abstract class. An abstract class can have variables, abstract method, a method body, constructor and main () method also. Here are some important points about  Abstract class.
  • An abstract class can have abstract methods and non-abstract methods.
  • If a class has an abstract method, this class must be an abstract class.
  • If we want to use an abstract class, it needs to be extended and its method implemented.
  • If we extend an abstract class, we have to provide an implementation for all the abstract methods in it.
  • We can’t instantiate an abstract class.
The complete program of an abstract class is listed below.
  1. abstract class AbstractClass {    
  2.     abstract void display();    
  3. }    
  4.     
  5. public class AbstractClassexample extends AbstractClass {    
  6.     void display() {    
  7.         System.out.println("Abstract class example");    
  8.     }    
  9.     
  10.     public static void main(String args[]) {    
  11.         AbstractClass p = new AbstractClassexample();    
  12.         p.display();    
  13.     }    
  14. }     
The above program generates the following output.
 
abstract-class-example-output
 

Interface 

 
An interface in Java is similar to a class and we can say that it is a blueprint of a class. Here are some important points about Abstract class.
  • The interface is mainly used to achieve full abstraction in Java.
  • The interface has static constants and abstract method body only and no method body.
  • Interface supports multiple inheritance in Java.
  • An interface shows the IS-A relationship also.
  • Interface can’t be instantiated, as in the case of an abstract class.
In Java, the compiler by default adds public and abstract keywords before the interface method and public, static and final keyword before the variables. It means that all the variables of an interface are public, static, final and methods are public and abstract. 
 
In Java, Multiple inheritances are not supported through the class. It is possible through an interface because of the interface methods implementation provided by the other class. Hence, there is no ambiguity. 
 
The complete program of the interface is listed below.
 
Step 1
 
First, we create a Java interface InterfaceExample in which we declare the method display(): 
  1. public interface InterfaceExample {  
  2.     public void display();  
  3. }  
Step 2
 
Second, we create a Java class PublicExample in which we declare the method display(): 
  1. public class InterfaceAccess implements InterfaceExample{  
  2.     @Override  
  3.     public void display() {  
  4.         System.out.println("Interface class access the interface named  InterfaceExample");  
  5.     }  
  6.   
  7.     public static void main(String args[]) {  
  8.         InterfaceAccess i = new InterfaceAccess();  
  9.         i.display();  
  10.     }  
  11. }  
The above program generates the following output.
 
interface-example-output
 

Differentiation between abstract class and interface in Java 

 
Abstract class  Interface 
An abstract class can hold abstract methods and non-abstract methods.  The interface has only abstract methods. 
Abstract keyword is used to create an abstract class.  The interface keyword is used to create the interface. 
Abstract class can contain static, non-static, final and non-final data members.  The interface has only static and final data members. 
An abstract class can contain static methods, the main method, and constructors.  Interface can’t contain static methods, main methods or constructors. 
An abstract class can give the implementation of an interface.  Interface can't give the implementation of an abstract class. 
An abstract class can extend only one class at a time.  The interface can extend any number of interfaces at a time. 
An abstract class is used to achieve partial abstraction.  The interface is used to achieve fully abstraction. 
An abstract class can extend from a class or from an abstract class. 
The interface can extend only from an interface. 
For example
 
public abstract class Car{
public abstract void run();
For example
 
public interface Bike{
Void run();
 

The similarity between Abstract class and Interface 

  • Both abstract class and an interface are used for abstraction in Java.
  • We can’t instantiate both.
The complete program of abstract class and interface.
 
Step 1
 
Creating an interface that has 4 methods. 
  1. public interface Shape {  
  2.     void draw();//bydefault, public and abstract  
  3.     void print();  
  4.     void show();  
  5.     void display();  
  6. }  
Step 2
 
Creating an abstract class that provides the implementation of one method of shape interface.
  1. abstract class Circle implements Shape {  
  2.     public void print() {  
  3.         System.out.println("Print image");  
  4.     }  
  5. }  
Step 3
 
Create a subclass of an abstract class. Now, we need to provide the implementation of the rest of the methods
  1. class Rectangle extends Circle {  
  2.     public void draw() {  
  3.         System.out.println("Draw image");  
  4.     }  
  5.     public void show() {  
  6.         System.out.println("Show image");  
  7.     }  
  8.     public void display() {  
  9.         System.out.println("Display image");  
  10.     }  
Step 4
 
Creating an Exampletest class that calls the methods of shape interface.
  1. public class ExampleTest {  
  2. public static void main(String args[]) {  
  3.         Shape a = new Rectangle();  
  4.         a.draw();  
  5.         a.print();  
  6.         a.show();  
  7.         a.display();  
  8.     }  
The above program generates the following output.
 
abstract-class-and-interface
 

Summary

 
In this article, we learned about abstract class and interface in Java programming language and how these are different from each other.