Java - Difference Between Abstract Class and Interface

By Shahnail Khan Last updated : March 22, 2024

In Java programming, both abstract classes and interfaces are used to create other classes. However, they have distinct characteristics and are used in different scenarios. Understanding the difference between abstract classes and interfaces is crucial for writing efficient and maintainable code.

Java - Abstract Class

An abstract class in Java is a class that cannot be instantiated on its own and may contain both abstract and concrete methods. Abstract methods are declared without a body, allowing subclasses to provide implementations. Abstract classes can also have fields, constructors, and concrete methods.

Example of Abstract Class in Java

Let's take an example to understand this better.

Suppose we are creating different types of vehicles. Each vehicle has common properties like speed and color, but they also have specific behaviors like how they accelerate. We can use an abstract class to represent this:

abstract class Vehicle {
  private String color;
  private double speed;

  public Vehicle(String color, double speed) {
    this.color = color;
    this.speed = speed;
  }

  public abstract void accelerate();

  public void displayInfo() {
    System.out.println("Color: " + color);
    System.out.println("Speed: " + speed + " km/h");
  }
}

class Car extends Vehicle {
  public Car(String color, double speed) {
    super(color, speed);
  }

  @Override
  public void accelerate() {
    System.out.println("Car is accelerating...");
  }
}

class Bicycle extends Vehicle {
  public Bicycle(String color, double speed) {
    super(color, speed);
  }

  @Override
  public void accelerate() {
    System.out.println("Bicycle is pedaling faster...");
  }
}

public class Main {
  public static void main(String[] args) {
    Vehicle car = new Car("Red", 60.0);
    car.displayInfo();
    car.accelerate(); // Output: Car is accelerating...

    Vehicle bicycle = new Bicycle("Blue", 20.0);
    bicycle.displayInfo();
    bicycle.accelerate(); // Output: Bicycle is pedaling faster...
  }
}

The output of the above example is:

Color: Red
Speed: 60.0 km/h
Car is accelerating...
Color: Blue
Speed: 20.0 km/h
Bicycle is pedaling faster...

In this example:

  • We have an abstract class called Vehicle, which represents common features of vehicles like color and speed. It also declares an abstract method accelerate(), which will be implemented differently by each type of vehicle.
  • Concrete subclasses like Car and Bicycle extend the Vehicle class and provide their implementations for the accelerate() method.

Java - Interface

An interface in Java is a reference type that contains only abstract methods, constants, and default methods. It serves as a contract for what a class can do, without specifying how it does it. Interfaces are used to achieve abstraction and multiple inheritance in Java.

Example of Interface in Java

Let's take an example to understand this better.

interface Switchable {
  void turnOn();
  void turnOff();
}

class Light implements Switchable {
  @Override
  public void turnOn() {
    System.out.println("Light is on");
  }

  @Override
  public void turnOff() {
    System.out.println("Light is off");
  }
}

class Fan implements Switchable {
  @Override
  public void turnOn() {
    System.out.println("Fan is on");
  }

  @Override
  public void turnOff() {
    System.out.println("Fan is off");
  }
}

public class Main {
  public static void main(String[] args) {
    Switchable light = new Light();
    light.turnOn();
    light.turnOff();

    Switchable fan = new Fan();
    fan.turnOn();
    fan.turnOff();
  }
}

The output of the above example is:

Light is on
Light is off
Fan is on
Fan is off

In this example:

  • We have an interface called Switchable, which defines two methods: turnOn() and turnOff(). Any class that implements this interface must provide implementations for these methods.
  • Concrete classes like Light and Fan implement the Switchable interface and provide their implementations for turning on and off.

Difference Between Abstract Class and Interface in Java

Sr. No. Abstract Class Interface
1. Can have both abstract and concrete methods. Contains only abstract methods.
2. Can have constructors. Cannot have constructors.
3. Can have instance variables. Cannot have instance variables.
4. Supports single inheritance. Supports multiple inheritance.
5. Use "extends" keyword to inherit. Use "implements" keyword to implement.
6. Can provide method implementations. Cannot provide method implementations.
7. Used to achieve partial abstraction. Used to achieve full abstraction.
8. Allows access modifiers for methods. Methods are implicitly public.
9. Cannot achieve multiple inheritance. Achieves multiple inheritance through interfaces.
10. Can be partially abstract or fully concrete. Must be fully abstract.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.