What is abstract class?

  • Abstract class is a class. In which we used abstract keyword with class or method. Not used with a variable.
  • Abstact class is inherited by other class.
  • You can’t use the abstract classes to created objects with directly. Instead, you have to inherit a class from the abstract class first.
  • Abstract class may or may not contain abstract method.
  • We can’t create the object of abstract class directly.
  • In abstract class, abstract method only declared.
  • Which class extends the abstract class it must provide the body of the abstract method of abstract class.
  • Abstract class is extended by other class.
  • Abstract method is terminated by the semicolon.
  • File name : index.php

    <?php
    abstract class Novel
    {
    function get_publisher()
    {
    return "hi <br>";
    }
    abstract function get_data();
    }
    class Test extends Novel
    {
    public function get_data()
    {
    return "it is the body of abstract method";
    }
    }
    $obj = new Test();
    echo "Hello ", $obj->get_publisher();
    echo "The novel says '", $obj->get_data(), "'<br>";
    ?>
    Output :
    Hello hi The novel says 'it is the body of abstract method'


    Example :-
    abstract class abc { public function xyz() { return 1; } }
    $a = new abc();//this will throw error in php

    Abstract classes in php are only for inheriting in other class.

    abstract class testParent
    {
    public function abc(){
    //body of your funciton
    }
    }
    class testChild extends testParent{
    public function xyz(){
    //body of your function
    }
    }
    $a = new testChild();

    Implementation of abstract method

    As we know that abstract functions are those functions of abstract class which is only defined. It will be declared in your child class. You can create any method abstract using keyword abstract. You can only create abstract method either in abstract class or interface. Following is example of the abstract method implementation:

    File name : index.php

    abstract class abc{
    abstract protected function f1($a , $b);
    }
    class xyz extends abc{
    protected function f1($name , $address){
    echo "$name , $address";
    }
    }
    $a = new xyz();


    In class abc we have defined an abstract function f1. Now when we have inherited class abc then declared function f1. If you have an abstract method in your abstract class then once you inherit your abstract class then it is necessary to declare your abstract method. If you will not declare your abstract method then PHP will throw error in that case.

    You can declare your abstract method in child class with the same visibility or less restricted visibility.

    File name : index.php

    abstract class parentTest{
    abstract protected function f1();
    abstract public function f2();
    //abstract private function f3(); //this will trhow error
    }
    class childTest{
    public function f1(){
    //body of your function
    }
    public function f2(){
    //body of your function
    }
    protected function f3(){
    //body of your function
    }
    }
    $a = new childTest();


    In above code you can see that you have declare 3 function in abstract class. But private declaration of the abstract method will always throw error. Because private method is availabe only in the same class context. But in case of f1. This is protected. Now in child class we have defined it as public because public is less restricted than protected. And for function f2 which is already public so we have defined it as public in our child class. We have defined it public because no any visibility is less restricted than public.

    An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract.

    Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created

    To define a class as Abstract, the keyword abstract is to be used e.g. abstract class ClassName { }

    File name : index.php

    abstract class Furniture {
    private $height, width, length;
    public function setData($h, $w, $l){
    $this->height = $h;
    $this->width = $w;
    $this->length = $l;
    }
    //this function is declared as abstract and hence the function
    //body will have to be provided in the child class public abstract function getPrice();
    }
    class BookShelf extends Furniture{
    private $price;
    public setData($h, $w, $l, $p)
    {
    parent::setData($h, $w, $l);
    $this->price = $p;
    }
    //this is the function body of the parent abstract method
    public function getPrice()
    {
    return $this->price;
    }
    }

    In the above example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the responsibility of the child class to provide the functionality of getPrice(). The BookShelf class is a child of the Furniture class and hence provides the function body for getPrice().


    Private methods cannot be abstract
    If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.
    abstract class BaseClass {private abstract function myFun();
    }
    class DerivedClass extends BaseClass {public function myFun() {//logic here }}
    $d = new DerivedClass(); //will cause error





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here