Choosing Between an Interface and an Abstract Class

Choosing Between an Interface and an Abstract Class

They accomplish the same result; it depends on what you?re trying to do

Image for postPhoto by Christina @ wocintechchat.com on Unsplash

During a few interviews, I?ve been asked to explain the difference between an interface and an abstract class and when I would choose one over the other.

Because this happened so much, I?m going to define each one and explain the difference between the two, and give an example of when to choose one over the other.

I will use Java to write sample code, but the concept should be similar in other languages as well.

What Is an Interface?

An interface is a behavioral contract between multiple systems.

That means any class that implements an interface guarantees and must provide some implementation for all of its methods. All methods in an interface must be public and abstract.

The interface definition looks like this:

public interface Movable { public void accelerate(); public void decelerate();}public interface Drivable{ public void drive();}

What Is an Abstract Class?

An abstract class is prefixed by the abstract keyword in its declaration and is a guideline created for its derived concrete classes. Abstract classes must have at least one abstract method and provide the implementation for its non-abstract methods.

If you define an abstract class with implementation, then you may need to consider whether going with an interface would be a better choice.

Abstract class definition looks like this:

public abstract class Vehicle { public void getReady(){ System.out.println(“I’m all set and ready to go”); } public abstract void start(); public abstract void stop(); }

To bring it all together, let?s create a Bus class that extends the Vehicle abstract class and implements both Drivable and Movable interfaces.

public class Bus extends Vehicle implements Drivable, Movable { @Override public void Drive() { System.out.println(“Driving…”); } @Override public void accelerate() { System.out.println(“accelerating…”); } @Override public void decelerate() { System.out.println(“decelerating…”); } @Override public void start() { System.out.println(“Starting engine…”); } @Override public void stop() { System.out.println(“Shutting down engine…”); }}

Differences and Similarities

All methods in an interface are public and abstract implicitly. Abstract classes can use public, partial, protected, and static access modifiers for their methods.

Classes can implement multiple interfaces, but only one abstract class. Abstract classes can contain non-abstract methods.

They can both have methods, variables, and neither one can be instantiated. All variables declared in an interface are final, while an abstract class may contain non-final variables.

Which One to Choose

If you need to provide some implementation or need a base class, then an abstract class is the way to go.

For instance, maybe you want to make sure to initialize some variables in a class to perform logic in a helper method which all derived classes can use, choosing to go with an abstract class is the right choice.

If you need to provide additional behavior for your classes, then interfaces are the way to go.

The same functionalities can be accomplished with both, however, when considering coding standards, an interface helps you accomplish abstraction and polymorphism, which are two of the main four OOP principles.

It also makes it easier to keep your code loosely coupled instead of tightly coupled, which happens when high-level modules depend on low-level modules.

Interfaces are also used for dependency injection (a.k.a. DI/IoC) and make it easier to mock derived classes while testing.

Thank you for reading. Happy coding!

23