java
  1. java-abstract-vs-interface

Java Abstract vs Interface

In Java, both abstract classes and interfaces can be used to implement abstraction, which is an essential concept in object-oriented programming. However, there are some significant differences between abstract classes and interfaces. In this article, we'll take a look at the differences between abstract classes and interfaces and help you decide when to use one over the other.

Syntax

Abstract Class

public abstract class Animal {
  public abstract void makeSound();
  public void eat() {
    System.out.println("Animal is eating");
  }
}

Interface

public interface Animal {
  void makeSound();
  void eat();
}

Example

Abstract Class

abstract class Animal {
   public abstract void makeSound();
   public void eat() {
      System.out.println("Eating...");
   }
}

class Dog extends Animal {
   public void makeSound() {
      System.out.println("Bark bark");
   }
}

class Main {
   public static void main(String[] args) {
      Animal myDog = new Dog();
      myDog.makeSound();
      myDog.eat();
   }
}

Output:

Bark bark
Eating...

Interface

interface Animal {
   void makeSound();
   void eat();
}

class Dog implements Animal {
   public void makeSound() {
      System.out.println("Bark bark");
   }
   public void eat() {
      System.out.println("Eating...");
   }
}

class Main {
   public static void main(String[] args) {
      Animal myDog = new Dog();
      myDog.makeSound();
      myDog.eat();
   }
}

Output:

Bark bark
Eating...

Explanation

Abstract class

An abstract class is a class that cannot be instantiated. Its purpose is to provide a common interface for its subclasses. Abstract classes can have both abstract and non-abstract methods. Abstract methods are declared without an implementation, while non-abstract methods have an implementation.

Interface

An interface is like a blueprint of a class. In an interface, all methods are declared without an implementation. The implementation is provided by the implementing class or classes. An interface can only have abstract methods, constants, and default methods.

Use

Abstract class

Use an abstract class when you want to provide a base implementation to your subclasses. Abstract classes allow you to define common methods and fields that all your subclasses can use. Abstract classes can also define abstract methods that all the subclasses must override.

Interface

Use an interface when you want to define a contract that all implementing classes must follow. Interfaces can define a set of methods that a class must implement, but it cannot provide the implementation. This allows you to define a set of methods that all the implementing classes must have.

Important Points

  • Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods.
  • An abstract class can provide a default implementation of a method, while an interface cannot.
  • A class can only extend one abstract class, but it can implement multiple interfaces.

Summary

Abstract classes and interfaces are both useful tools for implementing abstraction in Java. Use an abstract class when you need to provide a base implementation for your subclasses, and use an interface when you need to define a set of methods that all implementing classes must follow. By understanding the differences between abstract classes and interfaces, you can choose the best tool for the job.

Published on: