Abstract classes and Interface are very important concept in object oriented programming in PHP.
In this section we will look on following topics:
· What is abstract class?
· How to implement abstract class?
· What is interface?
· How to implement interface?
· Difference between abstract class and interface
What is abstract class?
Abstract classes are those classes which cannot be instantiated and require subclasses to provide implementation for the abstract methods or in other words you cannot create object of abstract classes and these classes are used for inheritance purpose. Abstract classes can only inherited in your child class.
An abstract class may contain one or more abstract method, but in PHP it may or may not contain abstract methods. Class Abstraction is a way to inherit another class to implement specific methods. In other words, when we extend any abstract class (parent class), all the methods in parent’s class must be defined by child class.
The parent class is called abstract class and the child class which is called as a concrete class. So, parent class force concrete class to implement specific methods and these methods are called abstract methods.
Some points to be remember
· If any class that has one abstract method, is termed as abstract class.
· An abstract class cannot be instantiated, you must instantiate child class or concrete class.
· An abstract method does not implement the method they only provide the abstract for the child class to implement.
How to implement abstract class?
You can create abstract class using keyword abstract in PHP. Once you have created abstract class then you cannot create object of that abstract class. Let’s see an example in which you have created the object of an abstract class:
<?php
abstract class test
{
public function abc()
{
return 1;
}
}
$obj = new test(); // Code will throw fatal error.
?>
Below is an example which shows how to implement abstract class:
<?php
abstract class My_parent
{
abstract public function text();
}
class Child extends My_parent
{
public function text()
{
echo "Hello World!! I am child class";
}
}
$obj = new Child();
$obj->text();
?>
What is interface?
Interface is similar to abstract class. As we all know PHP does not support multiple inheritance. This problem is solved by interface in which you group classes that share some functionality that do not necessarily share a parent class.
Interface is created using interface keyword. Following is a simple example of interface:
<?php
interface test
{
public function abc($a); // visibility should always be public
}
?>
So, in the above code you have created test interface which has a function abc(). When you will implement this test interface then it is mandatory for you to create method abc() in your class else it will throw error.
How to implement interface?
You can implement your interface by using implements keyword in your class. Following is an example how to implement your interface:
<?php
interface test
{
public function abc($a);
}
class child implements test
{
public function abc($a)
{
//your function body
}
}
?>
Following is an example that shows how interface solve the problem of multiple inheritance in PHP:
<?php
interface AName{
public function Add();
}
interface SName{
function Sub();
}
class CName implements AName,SName
{
public function Add()
{
$x = 5;
$x = $x + 1;
echo "The answer of Add function:".$x ."</br>";
}
function Sub()
{
$y=10;
$z=5;
$c = $y -$z;
echo "The answer of Sub function:".$c;
}
}
$obj= new CName();
$obj->Add();
$obj->Sub();
?>
Difference between abstract class and interface
- In abstract class it is not mandatory that every method should be abstract whereas in interface all methods are abstract, therefore all methods must be defined in class.
- Multiple and multilevel inheritance is possible in interface whereas in abstract only multilevel inheritance is possible.
- In interface methods should be public but in abstract it could be public and protected both.
- In interface you can define your method but in abstract you can define as well as declare methods.
Sunil Singh
29-Mar-2017