Interface and Abstract Classes

Filter Course


Interface and Abstract Classes

Published by: Dikshya

Published date: 30 Jun 2023

Interface and Abstract Classes

Interfaces and abstract classes are both mechanisms used in object-oriented programming to define common behavior and provide a level of abstraction. However, there are some key differences between them:

  1. Definition:

    • Interface: An interface is a contract that defines a set of methods that a class must implement. It specifies the method signatures but does not provide any implementation details.
    • Abstract Class: An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can contain both method declarations and method implementations.
  2. Multiple Inheritance:

    • Interface: A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.
    • Abstract Class: A class can only inherit from a single abstract class, as most programming languages do not support multiple inheritance for classes.
  3. Method Implementation:

    • Interface: All methods in an interface are implicitly abstract and do not have method bodies. Implementing classes provide the actual implementation.
    • Abstract Class: Abstract classes can have both abstract methods (without implementation) and concrete methods (with implementation).
  4. Constructors:

    • Interface: Interfaces cannot have constructors because they cannot be instantiated.
    • Abstract Class: Abstract classes can have constructors that are used when creating instances of their subclasses.
  5. Accessibility:

    • Interface: Interface methods are implicitly public and cannot have any access modifiers other than public.
    • Abstract Class: Abstract class methods can have various access modifiers like public, protected, or private.
  6. Usage:

    • Interface: Interfaces are often used to define a common set of behaviors that multiple unrelated classes can implement.
    • Abstract Class: Abstract classes are typically used to define common behavior and provide a base implementation that subclasses can extend.

Here's an example of an abstract class in Java:

// Abstract class
abstract class Animal {
    // Abstract method (no implementation)
    public abstract void makeSound();

    // Concrete method (with implementation)
    public void sleep() {
        System.out.println("Zzzz...");
    }
}

// Subclass of the abstract class
class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof!");
    }
}

// Subclass of the abstract class
class Cat extends Animal {
    public void makeSound() {
        System.out.println("Meow!");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();  // Output: Woof!
        dog.sleep();      // Output: Zzzz...

        Cat cat = new Cat();
        cat.makeSound();  // Output: Meow!
        cat.sleep();      // Output: Zzzz...
    }
}

In the example above, Animal is an abstract class that contains an abstract method makeSound() and a concrete method sleep(). The makeSound() method is declared as abstract in the Animal class, meaning it does not have an implementation in the abstract class itself. The Dog and Cat classes are concrete subclasses of the Animal class, and they provide their own implementation for the makeSound() method. They also inherit the sleep() method from the Animal class.

In the Main class, we create objects of Dog and Cat classes and call their respective methods. The makeSound() method outputs the sound specific to each animal, while the sleep() method is inherited from the Animal class.