---
title: "Abstract classes and Interface in PHP"  
description: "Abstract classes are those classes which cannot be instantiated and require subclasses to provide implementation for the abstract methods or in other"  
author: "Anonymous User"  
published: 2016-06-20  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11161/abstract-classes-and-interface-in-php  
category: "php"  
tags: ["php", "abstract class", "interface"]  
reading_time: 4 minutes  

---

# Abstract classes and Interface in PHP

Abstract [classes and Interface](https://www.mindstick.com/forum/34146/difference-between-abstract-classes-and-interface-in-php) are very important concept in object [oriented programming](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s) 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](https://www.mindstick.com/forum/33880/difference-between-abstract-class-and-sealed-class) [class and interface](https://www.mindstick.com/forum/95339/what-s-the-difference-between-an-abstract-class-and-interface-in-java)

##### 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:

```
<?phpabstract class test{public function abc(){return 1;}}$obj = new test(); // Code will throw fatal error.
?>
```

**Output:**

Fatal error: Uncaught Error: Cannot [instantiate abstract](https://www.mindstick.com/forum/23373/can-we-instantiate-abstract-class) class test // or you can say, it cannot create an object of abstract class(test)

Below is an example which shows how to implement abstract class:

```
<?phpabstract 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();
?>
```

**Note:** Myparent is an abstract class with abstract function text() which is implemented by Child class. Please note if you have an abstract method in abstract class and you have extend your abstract class then it is mandatory to declare your abstract method and [if you don](https://www.mindstick.com/forum/159346/how-is-the-exception-handled-if-you-don-t-catch-it)’t declare your abstract method then in that case PHP will throw error.

##### What is interface?

Interface is similar to abstract class. As we all know PHP does not support [multiple inheritance](https://www.mindstick.com/forum/159471/how-does-c-plus-plus-support-multiple-inheritance-and-what-are-potential-issues-associated-with-it). 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:

```
<?phpinterface 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:

```
<?phpinterface test{public function abc($a);}class child implements test{public function abc($a){//your function body}}?>
```

Please note while defining method in your interface don’t use abstract keyword.

Following is an example that shows how interface [solve the problem](https://answers.mindstick.com/qa/94744/why-is-google-play-store-not-completing-downloads-and-how-can-i-solve-the-problem) of multiple inheritance in PHP:

```
<?phpinterface 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();?>
```

**Output:**

The answer of Add function:6

The answer of Sub function:5

##### 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](https://www.mindstick.com/interview/23096/can-you-explain-me-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](https://www.mindstick.com/interview/2481/can-you-write-a-java-class-that-could-be-used-both-as-an-applet-as-well-as-an-application) declare methods.

---

Original Source: https://www.mindstick.com/blog/11161/abstract-classes-and-interface-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
