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.
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 |
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.
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:
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.
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:
The types of variables that are supported by the interfaces are as follows:
This post was last modified on January 2, 2021