Inheritance in OOP :

In this tutorial we will study about Inheritance.
  • Inheritance is a mechanism of extending an existing class.
  • In Inheritance child class inherits all the functionality of Parent Class.

For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods.

<?php


class BaseClass
{
    public function GetValue($string)
    {
        echo 'BaseClass:'. $string ;
    }


    public function setValue($string)
    {
        echo  $string;
    }


    
}


class ChildClass extends BaseClass
{
    public function GetValue($string)
    {
        echo 'ChildClass:'. $string ;
    }
}


$b = new BaseClass();
$c = new ChildClass();


$b->GetValue('Rajesh Test'); // Output: 'BaseClass: Rajesh Test'
$b->setValue('Rajesh Test'); // Output: 'Rajesh Test'
$c->GetValue('Rajesh Tutorials'); // Output:'ChildClass: Rajesh Tutorials'
$c->setValue('Rajesh Test'); // Output: 'Rajesh Test'


?>