Home Resource Centre 10+ Key Differences Between Abstract Class And Interface In Java

Java Programming Language Table of content:

10+ Key Differences Between Abstract Class And Interface In Java

In Java, both abstract classes and interfaces are used to define the structure of a class, but they serve different purposes and have distinct characteristics. Understanding the differences between these two is crucial for designing clean, efficient, and maintainable code. 

In this article, we will explore the key distinctions between abstract classes and interfaces, such as their syntax, usage, and when to choose one over the other. By the end, you'll have a clearer understanding of how each concept fits into object-oriented programming and when to apply them in your Java projects.

Differences Between Abstract Class And Interface In Java

An abstract class in Java programming is a class that cannot be instantiated and may contain both abstract methods (without implementation) and concrete methods (with implementation), serving as a blueprint for other classes. An interface, on the other hand, is a reference type that primarily defines a contract with abstract methods, though from Java 8, it can also include default and static methods. Here are the key differences between the two: 

Feature

Abstract Class

Interface

Keyword

Declared using abstract keyword.

Declared using interface keyword.

Methods

Can have both abstract and concrete methods.

All methods are abstract by default (except for default and static methods).

Constructor

Can have constructors.

Cannot have constructors.

Multiple Inheritance

A class can extend only one abstract class.

A class can implement multiple interfaces.

Access Modifiers

Methods can have any access modifier (private, protected, public).

Methods are implicitly public.

Fields

Can have instance variables (fields), including those with various access modifiers.

Can only have public, static, and final fields.

Inheritance

Supports single inheritance.

Supports multiple inheritance through interfaces.

Purpose

Used to share common behavior among related classes.

Used to represent a contract or capability that classes can implement.

Default Methods

Cannot have default methods (before Java 8).

Can have default methods (since Java 8).

Static Methods

Can have static methods with implementation.

Can have static methods with implementation.

Extending/Implementing

A class extends one abstract class only.

A class can implement multiple interfaces.

Explore this amazing course and master all the key concepts of Java programming effortlessly!

What Is An Abstract Class In Java?

An abstract class in Java is a class that cannot be instantiated directly. It serves as a base class for other classes to inherit from. Abstract classes can contain both abstract methods (methods without a body, which must be implemented by subclasses) and concrete methods (methods with a body that provide common functionality to subclasses).

Abstract classes are used when we want to provide a common template for a group of related classes, while allowing subclasses to implement or override specific methods as needed. They are a key feature of object-oriented programming (OOP) and help in enforcing a contract that subclasses must follow.

Key Points:

  • Cannot be instantiated: You cannot create objects of an abstract class directly.
  • Abstract methods: These are methods that are declared without an implementation. Subclasses must provide the method body.
  • Concrete methods: These are regular methods that can be defined within an abstract class.
  • Can have fields: Abstract classes can have instance variables (fields), which can be inherited by subclasses.
  • Can have constructors: Abstract classes can have constructors, which can be called by subclasses during initialization.
  • Single inheritance: A class can extend only one abstract class.

Abstract classes are often used when there is shared behavior among different classes but also the need for flexibility to define specific implementations in subclasses.

Code Example:

Output: 

This animal is eating.
The dog barks.
This animal is eating.
The cat meows.

Explanation: 

In the above code example-

  1. We begin by defining an abstract class Animal, which contains both a concrete method and an abstract method.
  2. The concrete method eat() provides a default implementation that prints "This animal is eating."
  3. The abstract method sound() doesn't have an implementation and must be defined by any subclass of Animal.
  4. We then define two subclasses, Dog and Cat, which extend the Animal class.
  5. Each subclass provides its own implementation of the sound() method. The Dog class overrides it to print "The dog barks," while the Cat class overrides it to print "The cat meows."
  6. In the Main class, we cannot instantiate the abstract class Animal directly, as shown by the commented-out line // Animal a = new Animal(); // Error.
  7. We create objects of the Dog and Cat subclasses and call the concrete eat() method (inherited from Animal) and the overridden sound() method, specific to each subclass.
  8. When we run the code, the output will display the respective messages for both the eat() and sound() methods when called on dog and cat.

Sharpen your coding skills with Unstop's 100-Day Coding Sprint and compete now for a top spot on the leaderboard!

What Is An Interface In Java?

An interface in Java is a reference type, similar to a class, but it can only contain abstract methods (methods without a body) and constants (fields that are static and final). Interfaces define a contract that any class implementing the interface must follow, ensuring that the class provides implementations for the abstract methods declared in the interface.

Starting from Java 8, interfaces can also have default methods (methods with a body that can be optionally overridden by implementing classes) and static methods.

Interfaces are widely used to achieve multiple inheritance in Java, since a class can implement multiple interfaces, allowing it to inherit behavior from more than one source.

Key Points:

  • Cannot be instantiated: You cannot create objects of an interface directly.
  • Abstract methods: By default, all methods in an interface are abstract, meaning they have no body.
  • Default methods: From Java 8 onwards, interfaces can have default methods with implementation.
  • Static methods: Interfaces can contain static methods with an implementation.
  • Constants: All fields in an interface are implicitly public, static, and final.
  • Multiple inheritance: A class can implement multiple interfaces, allowing it to inherit behavior from different sources.
  • No constructors: Interfaces cannot have constructors since they cannot be instantiated.

Interfaces are used when we want to specify a set of behaviors that classes must implement, regardless of where they sit in the class hierarchy.

Code Example: 

Output:

This animal is eating.
The dog barks.
This animal is eating.
The cat meows.

Explanation: 

In the above code example-

  1. We start by defining an interface Animal, which contains both an abstract method and a default method.
  2. The abstract method sound() doesn't have a body and must be implemented by any class that implements the Animal interface.
  3. The default method eat() provides a default implementation that prints "This animal is eating." Any class implementing the interface can use this method without needing to define it again.
  4. We define two classes, Dog and Cat, which implement the Animal interface.
  5. In both classes, we implement the abstract sound() method. The Dog class implements it to print "The dog barks," while the Cat class implements it to print "The cat meows."
  6. In the Main class, we create objects of the Dog and Cat classes, which are both valid because they implement the Animal interface.
  7. We then call the default eat() method (inherited from the interface) and the implemented sound() method for each object.
  8. When the code runs, the output will display the respective messages for both the eat() and sound() methods when called on dog and cat.

When To Use An Abstract Class?

An abstract class is used when you want to define a common base with shared functionality and enforce certain methods to be implemented by subclasses. It's useful when:

  • You have shared behavior (methods with implementation) that multiple subclasses can use.
  • You want to enforce specific methods (abstract methods) to be implemented by subclasses.
  • You need a class hierarchy where the parent class provides common properties or behaviors.
  • You want to prevent direct instantiation of a class.

Avoid using abstract classes when you need multiple inheritance (use interfaces instead) or don’t need shared functionality.

When To Use Interface?

An interface is used when you want to define a contract that multiple classes must follow, without dictating how they implement the methods. It's useful when:

  • You need to define a set of methods that multiple classes can implement, regardless of their class hierarchy.
  • You want to achieve multiple inheritance, as a class can implement multiple interfaces.
  • You want to define behaviors that can be shared across classes that might not be related by inheritance.

Avoid using interfaces when you need shared implementation or when you want to maintain a class hierarchy (use abstract classes instead).

Are you looking for someone to answer all your programming-related queries? Let's find the perfect mentor here.

Compatibility Between Abstract Class And Interface In Java

Abstract classes and interfaces can work together to provide flexibility in object-oriented design. A class can extend one abstract class and implement multiple interfaces, allowing it to inherit shared behavior from the abstract class and follow the contracts defined by the interfaces.

Code Example: 

Output: 

The dog barks.
This animal is eating.
The dog walks on four legs.
This mammal is sleeping.

Explanation: 

In the above code example-

  1. We begin by defining an interface Animal, which includes an abstract method sound() and a default method eat() that prints "This animal is eating."
  2. We also define an abstract class Mammal, which contains an abstract method walk() and a concrete method sleep(), which prints "This mammal is sleeping."
  3. Next, we define a class Dog that extends the Mammal class and implements the Animal interface. This class must implement the abstract methods from both the interface and the abstract class.
  4. In the Dog class, we override the sound() method from the Animal interface to print "The dog barks," and we override the walk() method from the Mammal class to print "The dog walks on four legs."
  5. In the Main class, we create an object of the Dog class, which now has access to methods from both the Animal interface and the Mammal abstract class.
  6. We then call the sound() method (from the Animal interface), the eat() method (the default method from the Animal interface), the walk() method (from the Mammal abstract class), and the sleep() method (from the Mammal abstract class).
  7. When the code runs, the output will display the respective messages for each of the methods called on the dog object.

Conclusion 

Abstract classes and interfaces are powerful tools in Java for designing robust and scalable applications. While abstract classes allow shared functionality and enforce a class hierarchy, interfaces provide flexibility by defining a contract that unrelated classes can implement. Understanding when to use each is key to writing clean, maintainable, and efficient code. By combining the strengths of both, developers can create systems that are both structured and flexible, leveraging the best of inheritance and polymorphism in Java.

Frequently Asked Questions

Q. Can a class extend an abstract class and implement an interface simultaneously?

Yes, a class in Java can extend a single abstract class and implement multiple interfaces. This allows the class to inherit shared functionality from the abstract class while adhering to multiple contracts defined by the interfaces. For example, a Bird class could extend an abstract class Animal and implement interfaces like Flyable and SoundMaker.

Q. What happens if I don't implement all methods of an interface in a class?

If a class does not provide implementations for all abstract methods of an interface, it must be declared as abstract. Otherwise, the code will fail to compile. This ensures that any concrete class fully honors the contract defined by the interface.

Q. Can an abstract class have constructors?

Yes, abstract classes can have constructors. These constructors are used to initialize fields or perform setup operations common to all subclasses. While you cannot instantiate an abstract class directly, its constructors are invoked when a subclass is instantiated.

Q. Are methods in an interface implicitly abstract and public?

Yes, methods in an interface are implicitly public and abstract unless explicitly declared otherwise. Starting from Java 8, interfaces can also include default methods (with a body) and static methods to provide additional functionality without breaking backward compatibility.

Q. When should I prefer interfaces over abstract classes?

Interfaces are ideal when you need to define a behavior that multiple, potentially unrelated classes must implement. For example, if you have classes like Car and Drone that need to implement a Flyable interface, using an interface is a better choice since these classes are not related by inheritance.

Q. Can I declare a field in an interface?

Yes, you can declare fields in an interface, but they are implicitly public, static, and final. This means they are constants and cannot be changed. For example, you can define a MAX_SPEED constant in an interface, but it cannot hold instance-specific data.

With this, we come to an end of our discussion of the key differences between abstract class and interface in Java programming. Here are a few other topics that you might be interested in reading: 

  1. Top 50+ Java Collections Interview Questions
  2. 10 Best Books On Java In 2024 For Successful Coders
  3. Difference Between Java And JavaScript Explained In Detail
  4. Top 15+ Difference Between C++ And Java Explained! (+Similarities)
  5. How To Find LCM Of Two Numbers In Java? Simplified With Examples
  6. How To Find GCD Of Two Numbers In Java? All Methods With Examples
Muskaan Mishra
Technical Content Editor

I’m a Computer Science graduate with a knack for creative ventures. Through content at Unstop, I am trying to simplify complex tech concepts and make them fun. When I’m not decoding tech jargon, you’ll find me indulging in great food and then burning it out at the gym.

TAGS
Java Programming Language Interview Preparation Engineering
Updated On: 8 Jan'25, 10:07 AM IST