July 11, 2022

Interface Vs Abstract Class in Java

Difference between Interface and abstract class in Java is a popular interview question. Since both abstract classes and interfaces are used to achieve abstraction in Java so it is important to know the differences between Interface and abstract class so that you know which of them to choose when.

Abstract class and interface in Java

As stated above both abstract class and interface are used to achieve abstraction. Both of them have abstract methods which are implemented by the classes implementing the interface/extending the abstract class. There are other similarities too like both of them can’t be instantiated.

With the way interfaces in Java are evolving by adding new features like ability to add default methods and static methods Java 8 onward and interface private methods Java 9 onward are making interfaces similar to abstract classes but the differences still remain so let’s go through the differences between Interface and abstract class in Java.

Interface Vs Abstract class in Java

  1. Abstract classes can have both abstract methods as well as methods with implementation.
    Interfaces, till Java 7, could only have public abstract methods. From Java 8 capability to add default and static methods to the interfaces was added but access modifier still had to be public. From Java 9 private methods can also be added to the interfaces in Java. So this difference between interface and abstract class is becoming narrower by the day. Only thing is abstract classes can have methods with protected access modifier as well as default (package-private) access where as interfaces can’t have such methods.
  2. Abstract classes can have fields that are not static and final.
    In interfaces, all fields are automatically public, static, and final and used to define constants.
  3. Abstract class can have constructors to initialize the fields in the abstract class.
    Since interface in Java can’t have instance variables so there is no need of constructor too to initialize those fields. Thus interfaces can’t have constructors. Here is an example showing the constructor in an abstract class.
    public abstract class Payment {
      private String customerName;
      private String customerAddress;
      public Payment() {
        
      }
      public Payment(String customerName, String customerAddress){
        this.customerName = customerName;
        this.customerAddress = customerAddress;
      }
      ..
      ..
    }
    
    class CashPayment extends Payment {
      CashPayment(String customerName, String customerAddress){
        super(customerName, customerAddress);
      }
      ..
      ..
    }
    
  4. Abstract class in Java can extend at most one class (abstract or non-abstract) but able to implement multiple interfaces.
    An interface can not extend a class though interface can extend multiple interfaces.
  5. An abstract class is extended by another class in Java. Extending class should implement all the abstract methods of the extended abstract class. If the class is not doing so then it should be declared as abstract itself.
    An interface is implemented by a class in Java. Implementing class should implement all the public abstract methods of the implemented interface. If the class is not implementing all the public abstract methods of the interface then it should be declared as abstract itself.

What to use – Abstract class or Interface

Now a very pertinent question is, which one should you use, abstract classes or interfaces?

You should consider using abstract class in one of the following scenarios-

  • If you have a set of closely related classes that share some common functionality and fields. In that case abstract class can have the common functionality as the implemented methods and abstract methods for what differs in these classes.
  • If you want to have protected or default access methods in the parent class along with abstract methods.
  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

You should consider using interface in one of the following scenarios-

  • You expect that unrelated classes would implement your interface. For example any library can have interfaces that are implemented by many unrelated classes in different applications.
  • You want to specify the behavior, a contract of a particular data type, but not concerned about who implements its behavior and how.
  • You want to have multiple inheritance as it is not possible with classes in Java.
That's all for the topic Interface Vs Abstract Class in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like

No comments:

Post a Comment