Understanding the Use of Interface and Abstract Classes

When and how to implement abstract and interface

Nahidul Hasan
Nov 26, 2017 · 3 min read
Image for post
Image for post
Photo by on

Interface Class

We know that an interface is defined by the interface keyword and all methods are abstract. All methods declared in an interface must be public; this is simply the nature of an interface.

Here is an example:

In an interface, the method body is not defined, just the name and the parameters. If we do not use an interface, what problems will be encounter? Why should we use interface?

You’ll get answers to these questions in this article. Please see the code below:

In the above example, I do not use interface. I write to the log using the LogToFile class.

But, if I want to write a log using LogToDatabase, I have to change the hard-coded class reference in the above code on line number 23.

That line code is:

public function __construct(LogToFile $logger)

This code will be:

public function __construct(LogToDatabase $logger)

In a large project, if I have multiple classes and I need to change them, I have to change all the classes manually.

But, if we use an interface, this problem is solved; we will not need to change code manually.

Now, see the following code and try to understand what happens if I use interface:

Now, if I change from LogToDatabase to LogToFile, I do not have to change the constructor method manually. In the constructor method, I have injected an interface; not an arbitrary class.

If you have multiple classes and swap from one class to another class, you will get the same result without changing any class references.

In the above example, I write the log using LogToDatabase and now I want to write the log using LogToFile.

I can call it like this:

$controller = new UsersController(new LogToFile);

I get results without changing other classes because interface handle this swapping issue.

Abstract Class

An abstract class is a class that is only partially implemented by the programmer. It may contain at least one abstract method, which is a method without any actual code in it — just the name and the parameters, and that it has been marked as “abstract”.

An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

Here is an example:

Now, the question is: “How do we know that a method will be needed and that it must be implemented?” I will try to explain this here.

Please see the Tea class:

Now, we look at the Coffee class.

In the two classes above, the three methods addHotWater(), addSugar(), and addMilk() are the same.

So, we should remove duplicate code. We can do that like this:

I make an abstract class and name it Template.

Here, I define addHotWater(), addSugar(), addMilk(), and make an abstract method named addPrimaryToppings.

Now, if I make the Tea class extend the Template class, I will get the three defined methods and I have to define the addPrimaryToppings() method. The Coffee class will as well, in a similar way.

And if you’d like to practice and get code about interface and abstract classes, please check out my (stars always appreciated) repository.

Thanks for reading.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store