KEMBAR78
Inheritance | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
8 views7 pages

Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows a derived class to acquire features and behaviors from a base class, enabling code reuse. The document explains various types of inheritance including single, multi-level, multiple (not supported in PHP), and hierarchical inheritance, along with examples in PHP code. Additionally, it covers method overriding, where a child class can provide a specific implementation of a method that differs from its parent class.

Uploaded by

bishowbabu45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows a derived class to acquire features and behaviors from a base class, enabling code reuse. The document explains various types of inheritance including single, multi-level, multiple (not supported in PHP), and hierarchical inheritance, along with examples in PHP code. Additionally, it covers method overriding, where a child class can provide a specific implementation of a method that differs from its parent class.

Uploaded by

bishowbabu45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Inheritance

Inheritance is a mechanism of acquiring the features and behaviors of a class by derived class.
Inheritance is one of the core features of object-oriented programming. It's a programming
procedure that allows to reuse code by referencing the behaviors and data of an object. In other
words, a class that inherits from another class shares all the attributes and methods of the base
class. An inherited class is defined by using the extends keyword.

<!DOCTYPE html>
<html>
<body>

<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

// Strawberry is inherited from Fruit


class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>

</body>
</html>

In above example, the strawberry class is inherited from the Fruit class. This means that the
Strawberry class can use the public $name and $color properties as well as the public
__construct() and intro() methods from the Fruit class because of inheritance. The Strawberry
class also has its own method: message().

Types of inheritance
Single inheritance
In this inheritance, a derived class is created from a single base class. In the given example,
Class A is the parent class and Class B is the child class since Class B inherits the features and
behavior of the parent class A.

<?php
class A
{
public function display()
{
echo "example of inheritance ";
}
}
class B extends A
{
public function view()
{
echo "in php";
}
}
$obj= new B();
$obj->display();
$obj->view();
?>

Multi-level inheritance
In this inheritance, a derived class is created from another derived class. In the given example,
class c inherits the properties and behavior of class B and class B inherits the properties and
behavior of class A. So, here A is the parent class of B and class B is the parent class of C. So,
here class C implicitly inherits the properties and behavior of class A along with Class B i.e. this
is a multilevel of inheritance.
<?php
class A {
public function fun1() {
echo "Hello from grandparent class.<br>";
}
}

class B extends A {
public function fun2() {
echo "Hello from Parent class.<br>";
}
}

class C extends B {
public function fun3() {
echo "Hello from child class.<br>";
}
}

$c = new C();
$c->fun1();
$c->fun3();
?>

Multiple inheritance
PHP does not support multiple inheritance (where a class can inherit from more than one class
directly). Instead, it uses traits to achieve similar functionality.
In the given example, class c inherits the properties and behavior of class B and class A at the
same level. So, here Class A and Class B both are the parent classes for Class C.
Hierarchical Inheritance
In this inheritance, more than one derived class is created from a single base class and further
child classes act as parent classes for more than one child class.
In the given example, class B and class C are derived from single base class A.

<?php
class A {
public function fun1() {
echo "Hello from Parent class.<br>";
}
}

class B extends A {
public function fun2() {
echo "Hello from first child class.<br>";
}
}

class C extends A {
public function fun3() {
echo "Hello from second child class.<br>";
}
}

$obj1= new B();


$obj1->fun1();
$obj2=new C();
$obj2->fun1();
?>

Function/Method Overriding

Function overriding is a technique of overriding the same method with same parameter in
parent and child class. In function overriding, both parent and child classes should have same
function name with and number of arguments. The purpose of overriding is to change the
behavior of parent class method with child class.
It provides a specific implementation of a method in a child class that differs from the
implementation in the parent class. This means that when we call the overridden method on an
object of the child class, the child class's implementation is executed instead of the parent
class's.

<?php
class P {
function display() {
echo "I am Parent Class";

}}
class C extends P {

// Overriding display method


function display() {

echo "I am Child class";

}
}
$p = new P;
$c= new C;
$p->display();
$c->display();
?>

You might also like