DIFFERENCE BETWEEN ABSTRACT CLASS AND INTERFACE IN PHP

What is abstract Classes

As from name it seem like something that is hidden. Yes nature of the abstract classes are same. Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Lots of people say that in abstract class at least your one method should be abstract. Abstract method are the method which is only defined but declared. This is not true definition as per my assumption. But your any class has at least one method abstract than your class is abstract class.

Usually, an abstract class is also known as base class. We call it base class because the abstract class is not the class which is available directly for creating an object. It can only act as the parent class of any normal class. You can use an abstract class in the class hierarchy. Mean one abstract class can inherit another abstract class also.

What is Interface?

Interface in oop enforce definition of some set of method in the class. By implementing interface you are forcing any class to must declaring some specific set of methods in oop. For example if you are creating class to render HTML element then it is necessary to set id and name of your html tag. So in this case you will create interface for that class and define method like setID and setName. So whenever someone will create any class to render HTML tag and implemented your interface then he must need to define setId and setName method in their class. In other word you can say that by help of interface you can set some definition of your object. Interface is very useful if you are creating architecture of any oop base application. Inter

Interface in PHP

Interface in php can be implemented like other oop lanugage. You can create interface in php using keyword interface. By implementation of interface in php class you are specifying set of the method which classes must implement.

S.No

Abstract Class

Interface

1

For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation. For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.

2

The Abstract methods can declare with Access modifiers like public, internal, protected. 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.

3

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

4

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.

 

 

 

 

 

 

 

 

 

 

 

 

 

Differences between abstract class and interface in PHP

Following are some main difference between abstract classes and interface in PHP

  1. In abstract classes, this is not necessary that every method should be abstract. But in interface every method is abstract.
  2. Multiple and multilevel both type of inheritance is possible in the interface. But single and multilevel inheritance is possible in abstract classes.
  3. The method of PHP interface must be public only. A method in an abstract class in PHP could be public or protected both.
  4. In an abstract class, you can define as well as declare methods. But in the interface, you can only define your methods.