GATE CSE IT » Difference Between Abstract Class and Interface in Java

Difference Between Abstract Class and Interface in Java

The distinction in Java between an abstract class and an interface. A model of an object is created using an abstract class, whereas the behavior that a class must implement is defined using an interface.

Abstract Class and Interface in Java

Java has two different abstraction representations: 

An abstract class and an interface:

  • A model for making objects is the abstract class. 
  • A class and its users enter into a contract known as an interface.

In contrast to interfaces, which serve as a contract between classes, abstract classes are used to generate reusable classes. In contrast to interfaces, which are used to specify a set of methods that a class must implement, abstract classes are useful for encapsulating shared functionality among several classes. While interfaces are better suited for specifying the contract between classes, abstract classes are frequently employed when numerous classes must share some common functionality.

Using interfaces, Java supports multiple inheritances

The only methods interfaces could define were abstract ones or methods without implementation. Therefore, it was not a problem if a class implemented several interfaces with the same method signature.

Interfaces and abstract classes are related concepts. They are not instantiable and may comprise a mixture of methods that have been declared but have not yet been implemented. 

The ability to declare non-static and final fields and public, protected, and private concrete methods are only available with abstract classes. A static method can neither be overridden nor implemented in a child class since it is a characteristic of the class rather than the object instance. Making a static method more abstract is, therefore, useless.

Java Abstract classes are capable of implementing interfaces even in the absence of interface methods. Java Abstract classes are utilized to give all subclasses’ default or common method implementation. Although there is a single superclass limit for Java classes, they can implement several interfaces. Because of this, a class with a separate superclass can implement interfaces but not extend other abstract classes. Interfaces are a more adaptable method of exposing a shared interface and other as well.

The term interface is used to declare an interface which only contains abstract methods that cannot be implemented. Java’s abstract keyword is used to create abstract classes when declaring the class. For classes and methods, the abstract keyword is a non-access modifier. Objects cannot be created using an abstract class, which is a constrained class. An abstract method do not have a form and can only be used in an abstract class.

In contrast to interfaces, which can only extend other interfaces, abstract classes can implement other classes and extend abstract interfaces. Although an interface cannot implement the main method, we can run an abstract class if it contains a main () function.

Difference between Abstract class and Interference

Abstract ClassInterference
Abstract classes have methods with implementation.Interference doesn’t have any implementation
Abstract classes are allowed to have constructors. Interference is not allowed to have constructors
An abstract class can be used to offer a method’s default implementation.Interference doesn’t offer a method’s default implementation
Abstract classes don’t include instances of any classes. Interferences include instances of any classes
An abstract class is capable of having instance variables. Interference is not capable of having instance variables
Java Abstract classes are capable of implementing interfaces even in the absence of interface methodsJava Interference isn’t capable of implementing abstract classes in the absence of abstract methods
Abstract methods are the only types of methods that can be included in an abstract class.Interference methods cannot be included in an abstract class.