Kubernate

Monday, November 10, 2025

0003.Difference Between Interface and Abstract Class

 An interface in Java is a reference type that contains only abstract methods and static constants. Starting from Java 8, interfaces can also contain default and static methods. From Java 9 onwards, interfaces can include private methods.


Abstract Class

An abstract class in Java is a class that cannot be instantiated on its own and is meant to be subclassed. It can have both abstract methods (methods without a body) and concrete methods (methods with a body).

2. Purpose

Interface

  • Provides a way to achieve full abstraction.
  • Defines a contract that implementing classes must follow.
  • Allows multiple inheritance of type.

Abstract Class

  • Provides a base class that other classes can extend.
  • Allows for code reusability by sharing common code among subclasses.
  • Can be partially abstract and partially concrete.

3. Key Differences

FeatureInterfaceAbstract Class
MethodsCan have abstract, default, static, and private methods (from Java 9).Can have both abstract and concrete methods.
FieldsCan only have static final constants.Can have instance variables and static fields.
Multiple InheritanceSupports multiple inheritance.Does not support multiple inheritance.
Access ModifiersMethods are implicitly public.Can have public, protected, and private methods.
Implementation InheritanceCannot inherit implementation.Can inherit implementation.
ConstructorCannot have constructors.Can have constructors.
InstantiationCannot be instantiated.Cannot be instantiated.
InheritanceCan be implemented by any class using implements keyword.Can be extended by any class using extends keyword.


4. When to Use Interface vs. Abstract Class

When to Use an Interface

  • When you need to define a contract that multiple classes should follow.
  • When you need to achieve multiple inheritance.
  • When you want to provide default implementation but allow for overriding.
  • When the functionality is intended to be implemented by various unrelated classes.

When to Use an Abstract Class

  • When you want to provide a common base class for related classes.
  • When you want to share common code among multiple classes.
  • When you have a combination of abstract and concrete methods.
  • When you need to define non-static or non-final fields that should be inherited.

Interface

Consider a scenario of electrical appliances:

  • An interface Switchable might define methods like turnOn and turnOff.
  • Different appliances like FanLight, and AirConditioner can implement the Switchable interface and provide specific implementations for turning on and off.

This allows any switchable appliance to be treated in a uniform manner, regardless of its specific type.

Abstract Class

Consider a scenario of a vehicle rental system:

  • The abstract class Vehicle might define common properties and methods like start and stop.
  • Specific implementations like Car and Bike extend Vehicle and provide specific implementations for those methods.

This allows shared behavior and properties to be inherited by all types of vehicles, while still allowing for specific differences.

Interfaces and abstract classes are powerful tools in Java that allow for different levels of abstraction and flexibility. Interfaces are best suited for defining a contract for unrelated classes and achieving multiple inheritance. In contrast, abstract classes are ideal for sharing common code among related classes and providing a base for extending classes. Understanding the differences and appropriate use cases for interfaces and abstract classes is essential for designing robust, maintainable, and flexible Java applications

No comments:

Post a Comment

Spring Boot - Bean LifeCycle

 Here is a clear, step-by-step lifecycle of a Spring Boot application , explained in a simple + interview-ready way. 🔄 Spring Boot Applica...

Kubernate