Abstract Classes and Interfaces in Java

Abstract Classes and Interfaces in Java

Abstract classes and interfaces in Java are fundamental in object-oriented programming (OOP). Abstract classes allow partial implementation, while interfaces define contracts that classes must follow.

1. Abstract Classes

An abstract class in Java is one that cannot be instantiated by itself. It provides as a foundation for subsequent classes. Abstract classes can include both abstract methods (without implementation) and concrete methods (with implementation).

Key Features:

  • Can have both abstract and non-abstract methods.
  • Can have constructors and member variables.
  • Supports access modifiers like public, protected, and private.
  • Subclasses can inherit common behaviors defined by the parent class.

Example:

abstract class Vehicle {
    int speed;
    Vehicle(int speed) {
        this.speed = speed;
    }
    abstract void start();
    void displaySpeed() {
        System.out.println("Speed: " + speed + " km/h");
    }
}

class Car extends Vehicle {
    Car(int speed) {
        super(speed);
    }
    @Override
    void start() {
        System.out.println("Car is starting");
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car(120);
        car.start();
        car.displaySpeed();
    }
}
When to Use Abstract Classes:
  • When code is shared across similar classes.
  • When specifying default behavior but leaving precise specifics to subclasses.

2. Interfaces

In Java, an interface is a completely abstract class that defines a contract to which all classes must adhere. It only supports abstract methods (until Java 8, when default and static methods were added).

Key Features:

  • Methods are public and abstract by default.
  • In contrast to classes, it allows for multiple inheritance.
  • Can include constants (public, static, and final).
  • Promotes loose coupling and improves testability.

Example:

interface Printable {
    void print();
}

interface Scannable {
    void scan();
}

class Printer implements Printable, Scannable {
    @Override
    public void print() {
        System.out.println("Printing document");
    }
    @Override
    public void scan() {
        System.out.println("Scanning document");
    }
}

public class Main {
    public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print();
        printer.scan();
    }
}

When to Use Interfaces:

  • When you need to do multiple inheritance.
  • When various classes must follow the same contract.
  • For specifying capabilities (e.g., Comparable and Serializable).

Key Differences Between Abstract Classes and Interfaces

  • Abstract classes can have constructors, whereas interfaces cannot.
  • Interfaces allow multiple inheritance, whereas abstract classes do not.
  • Abstract classes can store state (instance variables), whereas interfaces mostly specify behaviors.

Best Practices

  • Use abstract classes when related classes share code.
  • Interfaces are useful for specifying common behaviors between unrelated classes.
  • To promote flexibility, APIs should be designed with interfaces.

Understanding abstract classes and interfaces enables developers to create Java programs that are scalable, maintainable, and robust.

Categories: ,