Abstract Class and Interface (When to use)

Manndeep Vachhani
Geek Culture
Published in
3 min readJun 15, 2021

--

Interface vs Abstract Class

Theoretically, it comes easy to state what Abstract classes are and what Interfaces are in Java. This article should help you understand when you can use an Abstract class and when you can use an Interface when it comes to practice.

Here are the fundamental differences between an Abstract Class and an Interface :

From: https://www.tutorialspoint.com/Difference-between-Abstract-Class-and-Interface-in-Java

You know that right!

But when you are to decide on what you should use, these examples may help you.

First of all, think of entities you want to create in your project. Make a list of methods you would want to include in an Abstract class or an Interface.

I have thought of some, and this is my thought process to decide whether it will end up being an Abstract class or an Interface.

When to use Abstract Class :

Let’s take an example of a Superhero here, let’s create an abstract class “Superhero” with common methods that superheroes usually do.

Now, why abstract class here and not an interface? Because there can be child classes like Batman, Superman, Spiderman extending the SuperHero class. They all are heroes but we want to pass on specific things that superheroes pass on to child classes, the things that superheroes have to do.

It defines common behavior and attributes for the class. It’s more general or abstract. The subclass inherits common behavior but can override it in case the subclass needs a specific implementation of common behavior.

// Super class
public abstract class Superhero {
abstract void haveTragicPast(); abstract void saveTheWorld(); abstract void wearFancyDress(); abstract void makeDramaticEntrance(); abstract void haveSecretIdentity();

}
// Sub class
public class Batman extends Superhero {
}

You can use an Abstract Class when…
i) You want child classes of the parent Abstract Class to be enforced to override concrete methods.
ii) Philosophically, you can think of it as a parent Abstract Class giving instructions to child classes which they have to follow.
iii) You want to define a specific entity and limit its usage for sub-entities.
iv) You want the class to have code other than just abstract methods.

When to use Interface :

Interfaces facilitate the implementation of behavior that is not typical to the Class. This is a special behavior that the Class wants to have.
e.g. Superheroes can fly, airplanes can fly, birds can fly, some insects can also fly. In Indian households, footwear also can fly. But that doesn’t mean birds or insects(or footwear) are superheroes.

In this case, the contract for the behavior is defined in the interface. Any class that wishes to have this behavior needs to implement the interface and adhere to the contract.

So, there can be a FlyingBehavior interface.

public interface FlyingBehavior {
public void fly();
}
//Usage
public class Batman implements FlyingBehavior {
public void fly() {
System.out.println("I am batflying!");
}
}
public class Pigeon implements FlyingBehavior {
public void fly() {
System.out.println("I am flying n' shitting!");
}
}
public class AirPlane implements FlyingBehavior {
public void fly() {
System.out.println("I am flying. Thank you Jet Engines!");
}
}

You can use an Interface when…
i) You want to enforce the execution of Interface methods to any underlying class as an implementation.
ii) You don’t want to have a parent-child relationship in your implementation.
iii) You want to design a plugin, a set of events that can be delivered to any of the classes it is implemented by.
iv) You want to implement the common behavior of a specific entity.
v) You are sure that no code will be there in an interface other than abstract methods.

--

--

Manndeep Vachhani
Geek Culture

Caffeineaholic Thinker | Mobile Apps developer | Hoping to make lives better