Difference between Abstract class and Interface in Java

Short Explanation

Abstract classes and interfaces in Java are ways to achieve abstraction, but they work differently. An abstract class is a partial blueprint that can have both complete and incomplete methods. You can’t make objects from it directly. It’s great for sharing code among closely related classes. An interface, on the other hand, is a promise to include specific methods. Before Java 8, interfaces could only have abstract methods (no body), but now they can have default and static methods too. Classes can follow many interfaces at once, which allows Java to support multiple inheritances in a way. So, abstract classes are for when different objects do things in similar ways, and interfaces are for when objects do the same things but in different ways.

Detailed Explanation

In Java, both abstract classes and interfaces provide a way to achieve abstraction, which means to define the required functionality without implementing it. However, they serve different purposes and are used in different scenarios.

Abstract Class

  • Partial Implementation: Abstract classes can have both abstract (incomplete) and concrete (complete) methods.
  • Inheritance: A class can extend only one abstract class, which means Java doesn’t support multiple inheritances with abstract classes.
  • Access Modifiers: Methods in an abstract class can have any access modifier (public, protected, private).
  • Fields: Abstract classes can have fields that are final, non-final, static, or non-static.
  • Constructors: Abstract classes can have constructors.

Usage Scenario: When you have a base class that provides a common implementation for other classes, but also requires them to implement some specific functionality.

Interface

  • Abstract Methods: Before Java 8, interfaces could only contain abstract methods. From Java 8 onwards, they can also have default and static methods with a body.
  • Multiple Inheritance: A class can implement multiple interfaces, allowing Java to support multiple inheritances in this way.
  • Access Modifiers: All methods in an interface are implicitly public, and fields are public, static, and final.
  • Fields: Fields in an interface are final and static by default.
  • No Constructors: Interfaces cannot have constructors.

Usage Scenario: When you need to specify that a class must do certain things, but not how it does them, allowing for very different implementations.

Key Differences

  • Inheritance vs. Implementation: You extend an abstract class, but you implement an interface.
  • Method Types: Abstract classes can have a mix of method types, while interfaces have constraints on the types of methods they can declare.
  • Purpose: Abstract classes are used to capture the common characteristics of subclasses, whereas interfaces are used to define a contract that classes must follow.

Examples

Consider a scenario with shapes:

  • Abstract Class Shape: Might provide a method draw() that is abstract and methods like getColor() or setColor() that are concrete.
  • Interface Resizable: Contains abstract methods like resize(), minimize(), and maximize(), which any class can implement to become resizable regardless of its shape.

A class Circle could extend the Shape abstract class and implement the Resizable interface. This way, Circle inherits common properties from Shape and agrees to the contract of being Resizable.

Abstract Class vs. Interface in Java

FeatureAbstract ClassInterface
PurposeTo provide a common template for related classes.To define a contract for classes to implement.
Method ImplementationCan have both abstract and concrete methods.Prior to Java 8, could only have abstract methods. Java 8 and later allows default and static methods with implementation.
Access ModifiersMethods can have any access modifiers (public, protected, private).All methods are implicitly public (before Java 9). Java 9 introduced private methods for interfaces.
InheritanceA class can extend only one abstract class.A class can implement multiple interfaces.
FieldsCan contain non-final and non-static fields.Fields are implicitly public, static, and final.
ConstructorsCan have constructors.Cannot have constructors.
Use CaseWhen classes share a common structure or behavior.When unrelated classes need to implement a common contract.
Type of InheritanceSupports single inheritance.Supports multiple inheritance of type.
Method BodyCan have method bodies with or without an abstract keyword.Default methods can have a body. Other methods are abstract by default.
InstantiationCannot be instantiated directly, but can have a constructor.Cannot be instantiated at all.

Conclusion

Choosing between an abstract class and an interface depends on the design requirement. If you’re laying down a foundation that your classes will build upon, use an abstract class. If you’re defining a role that your classes can play, regardless of their place in the class hierarchy, use an interface. Understanding these differences is crucial for designing flexible and maintainable Java applications.