The difference between the interface and abstract is a very common question in core Java interviews. It is better to start answering this question with a slight introduction about abstract class and Interface.
What is Abstract Class?
Abstract classes are the class, which defines an abstract concept and it cannot be instantiated. Abstract classes are declared along with the keyword abstract. Creating objects in abstract classes is not allowed while inheritance is permitted.
- Many: Java interview Q&A available here
What is Interface?
Interface can be defined as the collection of abstract methods. Implementation of interface by a class is done by inheriting the abstract methods of a class. Interface is like a pattern it can assist classes through inheritance but cannot do anything itself.
Difference between Abstract Class and Interface in Java:
The knowledge of the difference between Abstract class and Interface in Java is a fundamental part of Java programming.
Extension of Classes
Abstract class is allowed to extend only one class or an abstract class at a time but interface is allowed to extend many number of interfaces at a particular time. The following programs will explain about extension of classes in Abstract class and interface.
Abstract Class
class MethodOne
{
public void displayOne()
{
System.out.println(“Display of MethodOne”);
}
}
abstract class MethodTwo
{
public void displayTwo()
{
System.out.println(“Display MethodTwo”);
}
}
abstract class MethodThree extends MethodOne
{
abstract void displayThree();
}
class MethodFour extends MethodTwo
{
public void displayThree()
{
System.out.println(“Post MethodThree”);
}
}
class DemoOne
{
public static void main(String args[])
{
MethodFour a=new MethodFour();
a.displayThree();
}
}
In the above program, MethodThree and MethodFour are extended from MethodOne and MethodTwo respectively in two separate steps. The output for the above program is method3.
Interface
interface MethodOne
{
public void displayOne();
}
interface MethodTwo
{
public void displayTwo();
}
interface MethodThree extends MethodOne,MethodTwo
{
}
class Method4 implements MethodThree
{
public void displayOne()
{
System.out.println(“Post Function Two”);
}
public void displayTwo(){
System.out.println(“Post Function Three”);
}
}
class DemoEd
{
public static void main(String args[])
{
MethodFour a=new MethodFour();
a.displayOne();
}
}
In the above program, MethodThree extends both MethodOne and MethodTwo using interface. The output for the above program is methodTwo.
Types of Class Extension
Abstract classes are extended from a class or an abstract class. Interface can be extended only from an interface. The following program will explain the extension of both abstract class and interface.
Abstract Class
class ExampleOne{
public void displayOne(){
System.out.println(“Post 1st method”);
}
}
abstract class ExampleTwo
{
public void displayTwo()
{
System.out.println(“Post 2nd Function”);
}
}
abstract class ExampleThree extends ExampleTwo
{
abstract void displayThree();
}
class ExampleFour extends ExampleThree
{
public void displayTwo(){
System.out.println(“ExampleFour<->displayTwo Function”);
}
public void displayThree()
{
System.out.println(“Post 3rd Function”);
}
}
class DemoEd
{
public static void main(String args[])
{
ExampleFour a=new ExampleFour();
a.displayTwo();
}
}
In the program given above the class ExampleThree is extended from abstract class ExampleTwo while class Example4 is extended from ExampleThree class. The output for above program will be displayTwo method.
Interface
interface ExampleOne
{
public void displayOne();
}
interface ExampleTwo extends ExampleOne
{
}
class ExampleThree implements ExampleTwo
{
public void displayOne()
{
System.out.println(“Post 1st method”);
}
}
class DemoEd
{
public static void main(String args[])
{
ExampleThree a=new ExampleThree();
a.displayOne();
}
}
In the above program, interface ExampleTwo is extended from the interface ExampleOne. The output for the above program is display1method.
Presence of Methods
Abstract class can have both concrete and abstract methods while interface can have only abstract methods. Use of concrete method is not allowed in interface. All the methods used in the interface are declared as abstract by default.
Abstract Class
abstract class ExampleOne
{
abstract void displayOne();
public void displayTwo()
{
System.out.println(“Post 2nd method”);
}
}
class ExampleTwo extends ExampleOne
{
public void displayOne()
{
System.out.println(“Post 1st method”);
}
}
class DemoEd
{
public static void main(String args[])
{
ExampleTwo a=new ExampleTwo();
a.displayOne();
}
}
In the above program, the use of both abstract and concrete methods like abstract void displayOne() and public void displayTwo() are clearly seen.
Interface
In the program given below, only the use of abstract method like public abstract void displayOne() is allowed. If concrete classes are used in interface, it will result in compiler error.
interface ExampleOne
{
public abstract void displayOne();
}
class ExampleTwo implements ExampleOne
{
public void displayOne(){
System.out.println(“Post 1st method”);
}
}
class DemoEd{
public static void main(String args[])
{
ExampleTwo a=new ExampleTwo();
a.displayOne();
}
}
Keyword
The use of keyword “abstract” is compulsory in abstract classes. The use of keyword “abstract” is optional in interface. The methods used in the interface are declared as abstract by default.
Abstract Classes
In the program given below, it is mandatory to use the keyword “abstract” to declare the method as an abstract. It is necessary because abstract classes use both abstract and compulsory methods.
abstract class ExampleOne
{
public abstract void displayOne();
}
class ExampleTwo extends ExampleOne
{
public void displayOne(){
System.out.println(“Post 1st method”);
}
public void displayTwo(){
System.out.println(“Post 2nd method”);
}
}
class DemoEd
{
public static void main(String args[])
{
ExampleTwo a=new ExampleTwo();
a.displayOne();
}
}
Interface
Interface will not allow the use of concrete methods so all the methods are declared as abstract by default. Hence no keyword is required.
interface ExampleOne{
public void displayOne();
}
class ExampleTwo implements ExampleOne
{
public void displayOne(){
System.out.println(“Post 1st method”);
}
public void displayTwo()
{
System.out.println(“Post 2nd method”);
}
}
class DemoDa
{
public static void main(String args[])
{
Exa2 a=new Exa2();
a.displayOne();
}
}
No keyword is used in the above program to declare the method as an abstract.