Difference Between Abstract Class and Interface in Java

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!
Learn via video course
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
By Tarun Luthra
Free
5
Enrolled: 1000
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
Tarun Luthra
Free
5
Enrolled: 1000
Start Learning

Overview

In Java, both abstract class and interface can declare a contract that the implementing class must satisfy. Defining a contract guarantees that the implementing class contains the properties we expect from it. Additionally, it is not possible to instantiate abstract classes and interfaces. However, an abstract class can declare constructs that interfaces cannot such as methods that are non-static and final, and methods that are private, protected, and public. Interfaces, on the other hand, extend the limitations of class inheritance in Java as one can simulate the effect of multiple inheritance through interfaces.

What is Abstract Class?

An Abstract Class in Java can assert its expectations for the implementing class by setting out abstract methods which guarantee that the class that intends to implement an abstract class contains the necessary implementations. Similar to Interfaces, an abstract class cannot be instantiated. Unlike an interface, a class can only inherit one abstract class.

An abstract class is especially useful when you want to share a set of properties among related classes. In addition, an abstract class supplements the limitations of defining method properties in an interface. One can define protected abstract methods/fields inside an abstract class which is not possible in interfaces (all fields/methods are public by default). Apart from this we can declare private non-abstract methods as well in abstract classes.

Note: There is no sense of making an abstract method private in an abstract class. It is because if an abstract method is declare private we cannot override/access it in the child class which will extend the abstract class and implement its abstract methods.

Properties of Abstract Classes

  • An abstract class cannot be instantiated. An abstract class can contain abstract methods which expect that the implementing class shall define.
  • An abstract class may be extended from another abstract class.
  • Unlike interfaces, a class that implements an abstract class can extend for only one abstract class.
  • Unlike interfaces, an abstract class can contain non-static methods, non-final methods, private and protected methods and attributes.

What is Interface?

An interface spells out the contract for the implementing class. We may think of it as writing your expectations of the class that intends to implement your interface. In other words, an interface articulates the agreement that you expect from the implementing class to conform.

Interfaces contain a high-level description of concepts that express their disambiguated meaning whenever it is being defined. You can think of an implementing class as a context that disambiguates the meaning of concepts defined in an interface.

Properties of an interface

  • An interface cannot be instantiated. All methods defined in the interface are implicitly public so one can omit the public access modifier in the interface definition.
  • An interface an extend any number of interfaces using the following syntax:
  • Prior to Java 8, all methods declared inside a Java interface must be abstract methods, i.e. they cannot have a body. However, since Java 8, an interface can have default and static methods too with their body defined.
  • All fields declared inside an interface are public, static and final by default.
  • Interfaces may contain static and final variables which are useful if you want to package a constant variable inside the interface which is available to all classes implementing the interface.
  • Interfaces do not contain constructors as they are not meant to be instantiated.
  • A class can implement multiple interfaces, i.e., a class can conform to the expectations of multiple interfaces.

Key Difference Between Abstract Class and Interface in Java

The main difference betweeen Abstract Class and Interface in Java is:

Abstract classes can have both abstract and non-abstract methods. Non-abstract methods provide a default implementation that can be used by subclasses. Abstract methods must be implemented by subclasses. Abstract classes can also have variables, both abstract and non-abstract.

In contrast, Interfaces can only have abstract methods. Interfaces cannot have any variables. Interfaces cannot be instantiated, but they can be implemented by classes.

Abstract classes can be used when you want to provide a base class with some common functionality and implementation that can be reused by subclasses. While Interfaces can be used when you want to define a set of methods that a class must implement, without providing any specific implementation.

Abstract Class vs Interface in Java

Abstract classInterface
Limits the implementing class to extend atmost one abstract classAllows a class to implement multiple interfaces which simulate the effect of multiple inheritance
Abstract classes can have abstract and non-abstract methods.Interfaces can only have abstract, static and default methods.
Supports non-final, final, non-static, and static methods and attributesSupports only final and static methods and attributes
Abstract classes can extend one abstract class and implement multiple interfaces.Interfaces can only extend other interfaces.
Can be inherited by other classes.Can be implemented by classes and other interfaces.
Can have constructors.Cannot have constructors.
An abstract class can provide the implementation of an interface.An interface cannot provide the implementation of an abstract class.
Cannot be instantiated.Can be instantiated.
Subclasses extend an abstract class using the extends keyword.Classes implement interfaces using the implements keyword.
[public] abstract class AbstractClass{}[public] interface InterfaceName{}
[public] class Concrete extends AbstractClass{}[pubilc] class Concrete implements Interface1, Interface2, ..., InterfaceN {}

Example of abstract class and interface in Java

Example of Abstract Class in Java

Let us consider a simple example of the Vehicle class:

In the above example, we notice that a vehicle is aptly represented as an abstract class since we intend to share the set of common properties in related concepts, i.e. vehicles. Furthermore, we intend to set some variables protected. An abstract class can inherit another abstract class as shown in the abstract class Car extends Vehicle portion of the example.

Example of Interface in Java

Let us consider a simple example of a Controllable interface:

In the above example, we demonstrate how interfaces can be declared and how implementing classes are expected to define the meaning of onLeft(), onRight(), onAbove(), and onBelow() in the context of the Car and Robot classes. As we observed, remote controls present an interface to the user, and our task as implementers are to sustain the meaning of actions defined in the interface in different contexts. Use this Java Online Compiler to compile your code.

When to use abstract class and interface in Java?

When to Use Abstract Classes?

Abstract classes are best used whenever you find your situation in one of the following:

  • Generate an abstract class if you wish to create several versions of your component. A straightforward and convenient method for adapting our components is provided by abstract classes. All inheriting classes are automatically updated with the change when the base class is modified. Conversely, once built, interfaces cannot be modified. If an interface needs to be updated, a new interface must be made.
  • You intend to declare non-static and non-final attributes and you require protected and private methods and fields.
  • Use an abstract class if you want to provide shared functionality across all implementations of your abstract class. In contrast to interfaces, which include no implementation for any members, abstract classes allow us to partially implement our class.
  • The benefit of abstract classes is that they enable better forward compatibility. Clients who have already used an interface cannot be changed; however, if they are using an abstract class, we can still add behaviour without causing the existing code to break.

Rules to be Followed for Abstract Classes

  1. An abstract class can contain constructors which set out its initial state. However, we cannot instantiate an abstract class.
  2. An abstract class can contain non-final, non-static, and default methods as well as final, static and abstract methods.
  3. An abstract class can contain public, private, and protected methods and attributes.
  4. An abstract class can contain abstract methods which impose the rule that a definition in the implementing class is expected.
  5. An abstract class can extend from another abstract class.
  6. A concrete class can extend only one abstract class.
  7. Like other classes, the naming convention for an abstract class is PacalCase.

When to Use Interfaces

Interfaces are best used in the following situations:

  • You intend unrelated classes to implement your interface. For instance, the Comparable and Clonable interfaces are implemented by unrelated classes.
  • You want to impose a set of regulated actions in a data type like adding the meaning of comparison via compareTo function on the implementation of a Comparable interface.
  • You want to simulate multiple inheritances as in specifying that you want your user-defined class to be comparable and clonable you list the interfaces that express these constructs or any situation that you require to share API contracts.

Rules to be Followed for Interface

  1. An interface is declared with the keyword interface i.e. interface Controllable{}
  2. The naming convention for an interface ends with -able which is reminiscent of an adjective since an interface contains a set of descriptions for the implementing class.
  3. Interfaces do not contain constructors.
  4. All methods inside the interface can either be abstract, static, final, or default (since Java 8).
  5. Fields and methods are implicitly public.
  6. Since Java 8, the default modifier may be added to a method that allows a method inside the interface to be defined as the default implementation. Before Java 8, all methods inside the interface are implicitly abstract which only specifies the method declaration without the body.
  7. Methods defaults to the public static final.
  8. Whenever a class implements an interface, it must provide the implementation of all abstract methods defined in an interface.
  9. An abstract class may implement an interface without even providing the implementation of all of its abstract methods.

Conclusion

  • Abstract classes and interfaces are two important concepts in Java that allow us to implement abstraction.
  • Abstract classes are suitable when you want to create a base class with both abstract and concrete methods, providing a blueprint for related classes to inherit common behavior and structure. They support single inheritance, can have constructors, and allow partial inheritance.
  • On the other hand, interfaces are used to define contracts for unrelated classes to implement shared functionality through abstract methods and constants. They support multiple inheritance, enforce full method implementation, and cannot have constructors.
  • The choice between abstract classes and interfaces depends on your design goals, balancing code reusability, structure enforcement, and flexibility in your application.