PHP  abstract classes

abstract is class is design just to achieve polymorphism.

you can create abstract classes and methods in php using abstract keyword.


in other word a class can be declared with the keyword abstract to indicate that it cannot be instantiated.


         abstract   class    cars{
                                          public   abstract   function    set_color( $c );
                                          public   abstract   function    get_color();

abstract method :- if a method has no definition then it is called abstract method.

  • you can not create object of abstract class
  • abstract class and method cannot be final
  • abstract method cannot be private, because private method are never inheritance.
  • if a class extends an abstract class then it must define all the abstract methods if any of the abstract method remain undefined in the subclass then the subclass must we declared abstract otherwise throw an error.
  • if the abstract method is declared as protected, the function implementation must be defined as either protected or public, but not private ( always less or same restricted).
  abstract   class       cars{
    public   abstract   function   set_color( $c );                            
      abstract   function   set_price( $p );//set_price method default visibility is public
    public   abstract  function   show(); 
               function     display(){  echo  "<br/>i am not a abstract method"; }                                             

       /* private  abstract   function   set_model( $mo );
        this function throw an error, because abstract method cannot be private */   

     /*  public function demo();  this will throw an error, because missing the abstract keyword*/
   /* final public abstract function  fi(); abstract method can not be final */ 
      class    audi    extends  cars{
                                              private $color = null;
                                              private $price = null;
               public  function  set_color( $c ){  $this->color = $c ; }
               public  function  set_price( $p ){  $this->price = $p ; }
               public  function  show(){               
                                                 echo "<br/> car color = ".$this->color;
                                                 echo "<br/> car price = ".$this->price;                          
                           // $obj  =  new  cars() this statement throw error in php
                                         $audi =  new  audi();
                                         $audi->set_color( "green" );
                                         $audi->set_price( "$44993" );
                                         $audi->show();   $audi->display();                              
   // final abstract class demo{this will throw error because abstract class cannot be final}
   abstract  class   cars{
                                  public     abstract function  set_color( $c );
                                  protected  abstract function  get_color();
            class   audi  extends  cars{
                                                  private   $color = null ;                                      
            public   function  set_color( $co ){   $this->color = $co ; }
            public   function  get_color(){   echo  " audi color = ". $this->color; }
      // above get_color visibility modifier can be protected or public but not private.
     //private  function  get_color(){} this is not less restricted show it will throw an error                                

                              $audi  = new  audi();
                              $audi->set_color( "audi" );
                                        if ( $audi  instanceof audi); // this will return true
                                        if ( $audi  instanceof cars); // this will return true