Interfaces – the next level of abstraction
Interfaces resemble abstract classes in that they include abstract methods that the programmer must define in the classes that inherit from the interface. In this way, interfaces contribute to code organization because they commit the child classes to the methods that they should implement. The use of interfaces become very helpful when we work in a team of programmers and want to ensure that all the programmers write the methods that they should work on, or even in the case of a single programmer that wants to commit himself to write certain methods in the child classes.
An interface commits its child classes to abstract methods that they should implement.
How to declare and implement an interface?
We declare an interface with the interface keyword and, the class that inherits from an interface with the implements keyword. Let’s see the general case:
1 2 3 4 5 6 7 |
interface interfaceName { // Abstract methods } class Child implements interfaceName { // Defines the interface methods, // and may have its own methods. } |
In the simple example given below, we will create an interface for the classes that handle cars, which commits all its child classes to setModel() and getModel() methods.
1 2 3 4 |
interface Car { public function setModel($name); public function getModel(); } |
Interfaces, like abstract classes, include abstract methods and constants. However, unlike abstract classes, interfaces can have only public methods, and cannot have variables.
The classes that implement the interfaces must define all the methods that they inherit from the interfaces, including all the parameters. So, in our concrete class with the name of miniCar, we add the code to all the abstract methods.
1 2 3 4 5 6 7 8 9 10 11 |
class miniCar implements Car { private $model; public function setModel($name) { $this -> model = $name; } public function getModel() { return $this -> model; } } |
Can we implement more than one interface in the same class?
We can implement a number of interfaces in the same class.
We can implement a number of interfaces in the same class, and so circumvent the law that prohibits the inheritance from more than one parent class. In order to demonstrate multiple inheritance from different interfaces, we create another interface, Vehicle, that commits the classes that implement it to a boolean $hasWheels property.
1 2 3 4 |
interface Vehicle { public function setHasWheels($bool); public function getHasWheels(); } |
Now, our child class can implement the two interfaces.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/ The class implements two interfaces class miniCar implements Car, Vehicle { private $model; private $hasWheels; public function setModel($name) { $this -> model = $name; } public function getModel() { return $this -> model; } public function setHasWheels($bool) { $this -> hasWheels = $bool; } public function getHasWheels() { return ($this -> hasWheels)? "has wheels" : "no wheels"; } } |
What are the differences between abstract classes and interfaces?
We saw that abstract classes and interfaces are similar in that they provide abstract methods that can be implemented only in the child classes. However, they still have the following differences:
• Interfaces can include abstract methods and constants, but cannot contain concrete methods and variables.
• All the methods in the interface must be in the public visibility scope.
• A class can implement more than one interface, while it can inherit from only one abstract class.
Let’s summarize these differences in the following table:
Interfaces | Abstract classes | |
the code | abstract methods & constant | abstract methods -constants -concrete methods -concrete variables |
access modifiers | -public | -public -protected -privatet |
number of parents | The same class can implement more than 1 interface | The child class can inherit only from 1 abstract class |