An interface and abstract class are used for achieve abstraction feature in Java. Abstraction is one of the OOP’s feature that aims on hiding background details and shows only the essential details.
The basic difference between abstract class and interface is that an abstract class can have abstract and non-abstract methods, whereas, an interface supports only abstract methods.
Differences between abstract class and Interface in Java With Comparison Table
Abstract Class | Interface |
---|---|
It supports abstract methods, static methods, final methods, and concrete methods. In other words, it can have both abstract and non-abstract methods | It supports abstract methods only. Java 8 onwards, it can have Default method and static method too. |
It doesn't support multiple inheritance | It supports multiple inheritance. An interface can extend two or more interfaces at a time |
An abstract class can extend single class only, but can implement one or more interfaces | An interface can extend multiple interfaces only |
It can have constructors | Interfaces don't |
'abstract' keyword is used to make a class abstract | 'interface' keyword is used to declare an interface |
It allows static, non-final, final and non-static variables | It allows static and final variables |
An abstract can have all types of members that involves private ,public, and protected | An interface can have public members only |
More Differences :-
- Static methods of an interface are accessed by interface name [interfaceName.StaticMethod()], while in case of abstract classes, static methods are simply accessed by class name [className.StaticMethod() , Subclassed name, not abstract Class name ].
Interface
An interface may consist of constants, default methods, abstract methods (without body )and static methods. It explains what a class should do and look like, but not how. It can be extended by other interfaces using ‘ extends’ and implemented by other classes using ‘implements’ keyword. In Java 8, a Default Method concept is added which allows implementation in that very method.
Java program to demonstrate the working of Interface
Program:
interface ShowingInterface{ int X= 20; // Constant as by default compiler would read it like this static final int x = 20 default void print(){ System.out.println("I am in default method, printing constant = "+X); } static void staticMethod(){ // Static method System.out.println("I am in Static method"); } void display (); // abstract method by default compiler would read it like this abstract void display (); } public class Interfaces implements ShowingInterface { @Override public void display(){ System.out.println("I am in abstract method"); } public static void main(String[] args) { Interfaces obj = new Interfaces(); obj.display(); // calling Abstract method obj.print(); // calling Default method ShowingInterface.staticMethod(); // calling static method } }
Output:
Abstract Class
It cannot be instantiated and is defined with abstract keyword. Extending classes must provide definition for all the abstract methods, otherwise, it also need to be declared as abstract.
It can have abstract methods, non-abstract methods and static methods.
Java program to demonstrate the working of abstract classes
abstract class showingAbstract{ // variable int variable; // constructor showingAbstract(){ variable=10; System.out.println("I am in constructor of abstract class right now "); } // abstract method abstract void print(); // static method static void display(){ System.out.println("I am in static method"); } // concrete method void showVariable(){ System.out.println("I am concrete method, Value of variable = "+variable); } } public class AbstractClass extends showingAbstract { @Override void print(){ System.out.println("Abstract method is executing "); } public static void main(String[] args) { AbstractClass obj = new AbstractClass(); obj.showVariable(); obj.print(); AbstractClass.display(); } }
Output:
Key differences
- An Abstract class can have abstract methods as well as the non-abstract methods; whereas; an interface can have only abstract methods.
- The concept of Multiple inheritance is not supported by the abstract classes because it causes ambiguity; whereas; for the interfaces, they allow multiple inheritance concept as there is no case ambiguity in their implementation.
- The types of variables that are supported by the abstract class are as follows:
- Static variable
- Non – static variable
- Final variable
- Non – final variable
The types of variables that are supported by the interfaces are as follows:
- Static variable
- Final variable
- The interesting fact about abstract classes is that the abstract class supports the implementation of the interfaces inside it; whereas; but the vice-versa does not happen, that is, the interfaces do not support the implementation of the abstract classes.
- In the case of Abstract class, it can be declared using the “abstract” keyword; whereas; in the case of Interface, if we want to declare an interface then you must be using the “interface” keyword.
- When we talk about abstract class, then to extend the abstract class functionality we must use the “extends” keyword; whereas; for the interfaces, if we want to implement the interface we must use the “implements”.