Abstract class vs Interface

Eltac Shixseyidov
3 min readAug 8, 2023

--

Today, we’re diving into the exciting world of programming, where we’ll unravel the mystery behind two important concepts that sometimes puzzle even the grown-ups: abstract classes and interfaces. These are like building blocks that help us create amazing things in the world of Java. People often mix them up, but fear not, we’re here to make it all crystal clear, just like solving a fun puzzle together!

Abstract Class

An abstract class is like a blueprint for creating other classes. It’s a class that cannot be instantiated on its own, but it provides a structure that other classes can follow. Think of it as a partially built house — you can’t live in it, but you can use it as a starting point to build a complete house.

Here’s how you might define an abstract class:

abstract class Animal {
abstract void makeSound();

void sleep() {
System.out.println("Animal is sleeping");
}
}

In this example, Animal is an abstract class with an abstract method makeSound() and a regular method sleep(). Any class that extends Animal must provide an implementation for makeSound(), and they'll inherit the sleep() method.

Interface

An interface is like a contract that a class must follow. It defines a set of methods that a class must implement, but it doesn’t provide any implementation itself. Think of it as a promise — if a class says it implements an interface, it’s promising to have certain methods available.

Here’s how you might define an interface:

interface Eater {
void eat();
}

class Dog implements Eater {
public void eat() {
System.out.println("Dog is eating");
}
}

In this example, Eater is an interface with a method eat(). The Dog class implements the Eater interface and provides an implementation for the eat() method.

Putting It Together

Now, let’s say we have a concrete class that combines both an abstract class and an interface:

class Lion extends Animal implements Eater {
public void makeSound() {
System.out.println("Roar!");
}

public void eat() {
System.out.println("Lion is eating");
}
}

Many developers believe that interfaces and abstract classes are similar, but they are actually quite different. Let’s explore the main differences between them.

Differences between abstract classes and interfaces

From an object-oriented programming perspective, the main difference between an interface and an abstract class is that an interface cannot have state, whereas the abstract class can have state with instance variables.

Another key difference is that classes can implement more than one interface, but they can extend only one abstract class. This is a design decision based on the fact that multiple inheritance (extending more than one class) can cause code deadlocks. Java’s engineers decided to avoid that.

Another difference is that interfaces can be implemented by classes or extended by interfaces, but classes can be only extended.

It’s also important to note that lambda expressions can only be used with a functional interface (meaning an interface with only one method), while abstract classes with only one abstract method cannot use lambdas.

Summary:

  • Use an abstract class when you want to provide a common base with some default behavior for subclasses.
  • Use an interface when you want to define a contract that classes must adhere to, without specifying any implementation details.
  • You can extend only one class (including abstract classes), but you can implement multiple interfaces.

By using abstract classes and interfaces, you can create well-structured, reusable, and flexible code in Java.

--

--

Eltac Shixseyidov
Eltac Shixseyidov

No responses yet