Share
What are the differences between abstract classes and interfaces? When do you need to use one, and when the other?
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
Abstract Classes VS Interfaces
by Alex
Abstract Classes VS Interfaces

Hey,
Alex here.

Abstract classes and interfaces are two advanced PHP OOP constructs.
They both work as blueprints for other classes.

In the case of abstract classes, other classes can use inheritance to become specific types of the parent, abstract class.
In the case of interfaces, classes can implement them to acquire specific functionalities.


But what are the differences between abstract classes and interfaces?
When do you need to use one, and when the other?

Let's find out in today's email.

P.S.
You can learn more about abstract classes and interfaces here:




Abstract Classes in a nutshell.

An abstract class is a class that is not complete.
You cannot create an object of that class.
Abstract classes can contain abstract methods: methods that only define their name and argument list (the so-called signature), but that lack the body.

You can define children classes that inherit from an abstract class. These children classes share the same type with the abstract class.
Children classes must implement the abstract methods from the parent class.


Interfaces in a nutshell.

Interfaces are similar to abstract classes, but they are not classes at all.
They provide a list of method signatures (name and argument list, without the body), which are much like abstract methods.
The purpose of interfaces is to provide a common set of functionalities for other classes.

Classes do not inherit from interfaces.
Instead, they implement them. When they do so, they must implement all the interface's methods and make them publicly available in the class.




The differences between Abstract Classes and Interfaces.

First of all, let's look at what abstract classes and interfaces have in common:

  • They both work as blueprints for other classes.
  • You cannot create objects of abstract classes or interfaces.
  • They both provide abstract methods that other classes must implement.
  • They support inheritance: abstract classes can inherit from other abstract classes, and interfaces can inherit from other interfaces.


Now let's take a look at the most important differences:

  • Abstract classes support methods, constants and properties. Interfaces only support methods and constants.
  • In abstract classes, you can define abstract methods and non-abstract methods as well. Interfaces only provide abstract methods.
  • Abstract classes support public, protected and private methods, constants and properties. Interfaces only support public methods and constants.
  • PHP does not support multiple inheritance: a class can only inherit from one class. However, a class can implement multiple interfaces at the same time.



Usage differences: when to use one and when the other.

And now we've arrived at the most important question: when to use which?

In a nutshell, you want to use class abstraction and inheritance when you want more classes to share the same type.
If there are some attributes (properties and methods) that are common to a particular type, then class abstraction is the way to go.

For example, you can define an abstract class named Bird, and then define children classes such as Owl, Parrot and Pigeon.
You can find more details in this tutorial.


However, sometimes class abstraction does not really work well.
For instance, there are cases when completely different classes need to share the same set of functions, without sharing the same type.
In these cases, you want to use interfaces.

For example, let's say that you want some classes to be able to print their data for debug purposes.
You can define a Debuggable interface, which provides a printDebug() method.
All classes that implement Debuggable will implement that method, no matter their type.
This way, all those classes will provide the same printDebug() functionality, but they will be free to inherit from different parent classes.

You can find more details in this tutorial.



That's all for today.
Now send me a reply with your questions and let me know what you think. I would love to hear from you.

Until next time,
Alex



Share the knowledge

Did you like this email? Share it with your friends!
Click here to share it



My courses to improve your PHP skills

PHP courses


How do you write PHP code that is always secure from attacks?
In this course you will learn the defense techniques that really work, leaving nothing to chance.

Take a look and see for yourself.


Resources

Alex Web Develop - My tutorials.
Alex PHP café
- My Facebook group where to talk with me and other developers.

Backend vector created by svstudioart - www.freepik.com




You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign