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 Shewale'); // Output: 'BaseClass: Rajesh Shewale'
$b->setValue('Rajesh Shewale'); // Output: 'Rajesh Shewale'
$c->GetValue('Rajesh Tutorials'); // Output:'ChildClass: Rajesh Tutorials'
$c->setValue('Rajesh Shewale'); // Output: 'Rajesh Shewale'
?>