Both interfaces and abstract classes are used to achieve abstraction in Java, but they serve different purposes and have different rules and use cases. Understanding when to use each is crucial in designing scalable, maintainable, and extensible systems.

1.Interface

Definition:

An interface in Java is a blueprint of a class that contains only abstract methods (methods without a body) and constants. An interface is a contract that defines what a class can do, but not how it does it. Starting from Java 8, interfaces can also have default and static methods with concrete implementations.

Example:

interface Animal {

    // Abstract method

    void sound();

   

    // Default method (Java 8+)

    default void sleep() {

        System.out.println("The animal is sleeping");

    }

}




class Dog implements Animal {

    @Override

    public void sound() {

        System.out.println("Dog barks");

    }

}




public class Main {

    public static void main(String[] args) {

        Animal dog = new Dog();

        dog.sound();  // Output: Dog barks

        dog.sleep();  // Output: The animal is sleeping

    }

}

Advantages of Interface:

  • A class can implement multiple interfaces, overcoming the limitation of single inheritance in Java.
  • Interfaces promote loose coupling by defining behavior without specifying the implementation, allowing various classes to implement it in different ways.
  • By using interfaces, different classes can share the same method signatures but implement them in their own ways.

Uses:

  • Interfaces are used when different classes share the same behavior but may have completely different implementations. For example, List , Set, and Queue interfaces in the Java Collections Framework.
  • When a class needs to inherit behaviors from more than one type, interfaces allow the class to implement multiple contracts.

2.Abstract Class

Definition:

An abstract class is a class that cannot be instantiated and can have both abstract methods (without implementation) and concrete methods (with implementation). Abstract classes are used to represent common features of subclasses and provide a base implementation for some or all methods, leaving the rest to be implemented by subclasses.

Example:

abstract class Animal {

    // Abstract method

    public abstract void sound();

   

    // Concrete method

    public void sleep() {

        System.out.println("The animal is sleeping");

    }

}




class Dog extends Animal {

    @Override

    public void sound() {

        System.out.println("Dog barks");

    }

}




public class Main {

    public static void main(String[] args) {

        Animal dog = new Dog();

        dog.sound();  // Output: Dog barks

        dog.sleep();  // Output: The animal is sleeping

    }

}

Advantages of Abstract Class:

  • Abstract classes allow a combination of abstract and concrete methods. This allows them to define a default behavior, while subclasses can override or add new behaviors.
  • Code Abstract classes provide a way to define common behaviors and attributes that can be reused by subclasses.
  • Unlike interfaces, abstract classes follow the single inheritance rule, but they provide a more cohesive implementation for related classes.

Uses:

  • Abstract classes are used when a class should act as a base for related subclasses with shared attributes and behaviors. For example, Vehicle as an abstract class can define shared behaviors (like accelerate() and brake()) for different types of vehicles (Car, Bike).
  • When you want to provide some default behavior but enforce that some methods must be implemented by the subclass.

Categorized in: