Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Inheritance in OOP, Introduction, and Advantages

Inheritance in OOP, Introduction and Advantages

 

Inheritance in OOP, Introduction and Advantages:
Well, that’s a cool question and any OOP languages programmer has to know what is inheritance in OOP in order to reuse the existing class properties and methods. Inheritance in OOP is the process of deriving or acquiring the properties and characteristics of other classes that have been created as a template. When inheritance comes in mind there will be two classes.
Parent class: The class which allows other classes to derive its own properties and characteristics.
Child class: The class which derives the properties and characteristics of the parent class.

What are the types of inheritance?
OOPS supports the following types of inheritance.
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance

1. Single Inheritance
Single inheritance is where one class derives the other class properties and there will be only two class called as parent and child class.

// A base call for animal
class Animal 
{
protected $animalName;
protected $foodGroup;
public function __construct($animalName, $foodGroup) 
{
$this ->animalName = $animalName;
$this ->foodGroup = $foodGroup;
}
public function introduceAnimal() 
{
echo "Hi i am ".$this ->animalName." and i belong to ".$this ->foodGroup."\n";
}
public function eatFood()
{
echo "Hi i am having my food \n";
}
}

// cat is inheriting animal class
class Cat extends Animal 
{
public function greetEmployee() {
echo "Hello employee, how are you doing?\n";
}
}
$catObject = new Cat('Cat', 'Mammals');
$catObject ->introduceAnimal();
$catObject ->greetEmployee();

2. Multiple Inheritance
Multiple inheritances are where one class derives the more than one base class properties and there will be only one child class nut more than one base classes. I am writing my code in PHP and PHP doesn’t support multiple inheritances but by using trait instead of class you can implement.

// A base call for animal
class Animal 
{
protected $animalName;
protected $foodGroup;
public function __construct($animalName, $foodGroup) 
{
$this ->animalName = $animalName;
$this ->foodGroup = $foodGroup;
}
public function introduceAnimal() 
{
echo "Hi i am ".$this ->animalName." and i belong to ".$this ->foodGroup."\n";
}

}

// Trait forAnimal 
trait forAnimal 
{ 
public function eatFood()
{
echo "Hi i am having my food \n";
}
} 

// cat is inheriting animal class
class Cat extends Animal 
{
use forAnimal;
public function greetEmployee() {
echo "Hello employee, how are you doing?\n";
}
}
$catObject = new Cat('Cat', 'Mammals');
$catObject ->introduceAnimal();
$catObject ->greetEmployee();
$catObject ->eatFood();

//Output
Hi i am Cat and i belong to Mammals
Hello employee, how are you doing?
Hi i am having my food

3. Multilevel Inheritance
Multilevel inheritance is where a derived class is created from another derived class. Using regular class for multilevel inheritance doesn’t make more sense. So we are using the abstract class for the same.

abstract class firstLevelClass 
{

    abstract public function AssignTaskmployee();
    public function greetEmployee()
    {
        echo "Good morning\n";
    }
}

abstract class secondLevelClass extends firstLevelClass 
{
    abstract public function sayByeEmployee();
    public function AssignTaskmployee()
    {
        echo "Task has been assigned...\n";
    }
}

class thirdLevelClass extends secondLevelClass 
{
    public function sayByeEmployee()
    {
        echo "Good bye employee\n";
    }
}

$objectthirdLevelClass=new thirdLevelClass();
$objectthirdLevelClass->greetEmployee();
$objectthirdLevelClass->AssignTaskmployee();
$objectthirdLevelClass->sayByeEmployee();

// Output
Good morning
Task has been assigned...
Good bye employee

 

4. Hierarchical Inheritance
Hierarchical inheritance is where the base class will be having more than one child classes.

When to use inheritance in OOP?
When the objects of classes should strictly follow the behaviour of your base class.

Also, read What is polymorphism?

3 thoughts on “Inheritance in OOP, Introduction, and Advantages

Comments are closed.