PHP: Key difference between abstract class and interface

Difference between Abstract class and Interface PHP

 

An abstract class is a class that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class (parent) or else the child class also becomes abstract.

 

To define a class as Abstract, the keyword abstract is to be used

 

[php]

abstract class ClassName { }

[/php]

 

Abstract class must have at least one abstract method (only declared not defined).
If you want to use this class then you have to inherit it.

 

An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behavior (logics).

 

Abstract class and interface cannot be instantiated i.e. only concrete class can be instantiated
To extend from an Interface, a keyword implement is used.

 

Difference between Abstract Class and Interface:

 

An abstract class can provide some functionality and leave the rest for derived class to provide.
An interface cannot contain any functionality. It only contains definitions of the methods.

 

The derived class may or may not override the concrete functions defined in base abstract class (Not mandatory).
The derived class must provide code or login (method definition) for all the methods defined in the interface (Mandatory).

 

The child class extended from an abstract class should logically be related.
Completely different and non-related classes can be logically be grouped together using an interface.

 

For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation/definition.
In interface all the methods by default are abstract methods only so you need not use abstract keyword at all.

 

The Abstract methods can declare with Access modifiers like public, protected(not private).When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.
All methods declared in an interface must be public.

 

Abstract class can contain variables and concrete methods.
Interfaces cannot contain variables and concrete methods except constants.

 

A class can inherit only one Abstract class and multiple inheritance is not possible for Abstract class.
A class can implement many interfaces and multiple interface inheritance is possible.

 

You can use interface as marker (An interface having no methods is called as a Marker Interface)
You can also do in abstract class to making all the methods abstract but it is better always use interface as marker.

 

In general, prefer interfaces if you don’t need to use an abstract class, because they provide more design flexibility.

Note:

  • Private methods cannot be abstract because private method cannot be inherited. If a method is defined as abstract then it can only be public or protected.