Welcome
    

Single Inheritance

 

Single inheritance is a fundamental concept in object-oriented programming where a class can inherit properties and methods from only one parent class, also known as a superclass or base class. This means that a subclass, or derived class, can extend the functionality of a single parent class by inheriting its attributes and behaviors.

 

Key points about single inheritance:

 

Parent-Child Relationship

Single inheritance establishes a parent-child relationship between classes. The subclass is considered a specialized version of the superclass, inheriting its characteristics.

 

Code Reusability

Single inheritance promotes code reusability by allowing subclasses to inherit and reuse the code defined in the superclass. This reduces code duplication and facilitates better code organization.

 

Subclass Extension

Subclasses can extend the functionality of the superclass by adding new methods or properties, or by overriding existing methods to provide specialized behavior.

 

Example in PHP:

 

class Animal {
    protected $species;

    public function __construct($species) {
        $this->species = $species;
    }

    public function eat() {
        echo "Animal is eating.";
    }
}

class Dog extends Animal {
    public function bark() {
        echo "Dog is barking.";
    }
}

$dog = new Dog("Canine");
echo $dog->species;
// Output: Canine
$dog->eat(); // Output: Animal is eating.
$dog->bark(); // Output: Dog is barking.

 

In this example, the class Dog inherits from the class Animal. The Dog class inherits the species property and the eat() method from the Animal class. It also defines its own method bark(). This demonstrates single inheritance where Dog is a subclass of Animal.