Abstract Classes and Interface in PHP

Abstract Class
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 derived 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. But your any class has at least one method abstract than your class is abstract class.

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

You can create abstract classes in php using abstract keyword. Once you will make any class abstract in php you can not create object of that class.

<?php
abstract class testParent
{
public function abc()
{
echo 'Vineet';
}
}
class testChild extends testParent
{
public function xyz()
{
echo 'Saini';
}
}
$a = new testChild();
echo $a->abc();
echo $a->xyz();
?>

Interface

we knows that PHP does not support the multiple inheritance. But don’t worry we have an excellent idea to do the multiple inheritance in php is Interface.
Interface is just like a class using interface keyword and contains only function declarations(function with no body). function is defined in the classes where you call it.

Interface gets useful when you need to extend a class from more than one parent class since class can not do this.
An interface cannot contain any functionality. It only contains definitions of the methods

<?php
interface template1
{
public function f1();
}

class test implements template1
{
public function f1()
{
echo 'Vineet';
}
public function f2()
{
echo 'Saini';
}

}
$a = new test();
echo $a->f1();
echo $a->f2();
?>

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 interface. But single and multilevel inheritance is possible in abstract classes.
  3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.
  4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.
Advertisements
Categories: PHP & Joomla | Tags: , , , , , , | 2 Comments

Post navigation

2 thoughts on “Abstract Classes and Interface in PHP

  1. what is the benefit of abstract class..

    • Abstract classes created for inheritance purpose. We can only inherit abstract class in your derived class. Using an Abstract Class, we can provide some kind of default functionality for all child classes to extend from. This is useful to avoid code duplication.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: