Difference Between Abstract Class and Interface in Java

In designing, the most frequent design issue or a question that a designer has is "what is the difference between an abstract class and an interface? When to use an abstract class and when to use an interface?" This article explains the difference between an abstract class vs interface in java. The choice depends on class design.

Difference Between Abstract Class and Interface in Java

Abstract Class Interface
An abstract class can have method definitions and some other method declarations leaving definition to subclasses. An interface can have method declarations only and not definitions.
Variables in an abstract class can be static, final as well as instance variables. Variable in an interface are implicitly static and final.
A class can extend only one abstract class. A class can extend more than one interface and hence can used to implement multiple inheritance in java.
An abstract class is an incomplete class. An interface is a pure abstract class.
An abstract class can have a constructor. An interface cannot have a constructor.
An abstract class is extended using extends keyword. An interface is implemented by a class using implements keyword.

This difference is also same in PHP, so you can use this to implement Multiple Inheritance in PHP.

Keep reading I have explained below "When to use an interface and when to use an abstract class in java?"

An abstract class in java is a class which can contain data members, static fields,member function definitions and some other member function declarations leaving definition to subclasses. An abstract class is an incomplete class which means it cannot be instantiated like any other class.

An interface in java on the other hand is a pure abstract class which contains method declarations and some variables which are implicitly static and final. An interface only provides information about what behaviour a class (which implements that interface) will provide. An interface does not tell how to implement the behaviour. It does not provide any definition of the methods.

Now that we know what is an abstract class and what is an interface in java we can easily different between them. The first difference would be an abstract class contains more functionality already defined compared to an interface.

When to use an interface and when to use an abstract class?

You should use an interface when one or more classes need the same functionality of the interface. It is not necessary that there should be a relationship between the interface and the implementing class.

You may say that why not use an abstract class instead? Yes you can use an abstract class but that would inherit all other non necessary things already defined in the abstract class. If you need to create multiple classes like, student, teacher, HOD( head of the department) then you can define an abstract class named person, all other three classes can inherit from that because all other three classes are represent a person also.