Thursday, January 10, 2013

Inheritance

if you were creating an application to handle inventory at car dealership,you would probably need classes like Sedan,PickUpTruck ,and MinVan that would correspond to the same types of automobiles in the dealer's inventory.Your application would need not only to show how many of these items you have in stock but also to report on the characteristics of these vehicles so that the salespeople could give the information to customers.

 In This example,we will create another two files.

saved as class.inheritance.php
<?php
  class father{
    private $_car="benz";
    private $_properties="a house";
   
   
    public function getFatherDetail(){
        $total=" father : {$this->_car},{$this->_properties}";
        return $total;
    }

   
 }
   class child extends father{
       private $_toy="PS3";
       private $_friend="Billy";

       public function getChildDetail(){
           $total=" ||| Child : {$this->_toy},{$this->_friend}";
           return $total;
       }
   }
?>



saved as testdemo.php
<?php
    require_once("class.inheritance.php");
     $objFather=new father();
     echo $objFather->getFatherDetail();
     //echo $objFather->getChildDetail();
     /*if u uncomment "echo $objFather->getChildDetail(); ", u will receive a
     error message.  it is because Father couldnt inheritance anything from child,*/

     $objChild=new child();
     echo $objChild->getChildDetail();
     echo $objChild->getFatherDetail();
?>



output:

father : benz,a house ||| Child : PS3,Billy father : benz,a house

No comments:

Post a Comment