Searching...
Sunday 14 October 2012

Php Inheritance

10:20 pm

Php Inheritance

Php Inheritance :

1.      Inheritance is the process of creating a new concept from an already existing concept(s);
2.      Inheritance is introduced for reusability.
Types of inheritance:
Simple inheritance :
Simple inheritance
Herarchiel Inheritance:

Herarchiel Inheritance
Multi Level Inheritance :
Multi Level Inheritance
Multiple Inheritance :
Multiple Inheritance
Hybrid Inheritance :
=(2) + (3) + (4) 

Hybrid Inheritance
The newly created class is known as “sub-class” and the class from which it is created is known as parent class.
“extends” is the keyword used for creating a subclass.
Class B extends A
{
}
After “extends” keyword we can give only one class name thet means in php we cannot create a class from morethan one parent class in order to overcome ambiguity error .multiple inheritance is happening through inheritance.
<?php
class A
{
    public $a=10,$b=20;
    function addNum($x=0,$y=0)
    {
      $res = $x + $y;
      echo "sum=".$res."<br>";
     
    }
}
?>
<?php
class B extends A
{
    public $c=30,$d=40;
    function subNum($x=0,$y=0){
        $res=$x-$y;
        echo "difference=".$res."<br>";
        
    }
}
?>
<?php
echo "<h1>";
$obj = new B();
echo $obj->a."--".$obj->d."<br>";
$obj->addNum(11,22);
$obj->subNum(22,9);
?>
OutPut:
10--40
sum=33
difference=13           




0 comments:

Post a Comment