Object Oriented Programming(OOP) Series: Inheritance

Object Oriented Programming(OOP) Series: Inheritance

Inheritance in simple terms with illustrations

In this series as promised earlier we'll be brushing up OOP concepts in the most simplest form with examples in PHP, Javascript and Dart.

So far in this series we've covered Classes & Objects , Attributes & Methods, Constructor & Destructor, and Access modifier

Access modifiers(specifiers) are keywords or terms that defines the accessibility of classes, methods, and attributes. Access modifiers varies according to the syntax used by OOP languages to enhance the security or coverage of data members and methods.

Inheritance

In programming there are some concepts that apply to real life scenario. Like we said earlier, that OOP is about representing real life scenario in object form. More so since we said that a class is the parent of an object (child) it means that there are some attributes and behavior that are found in the child that can be traced to the parent.

Inheritance: is a terminology that describes the passing of certain features and behavior from a parent to a child (this is not restricted to human beings alone all living things have this). Those of us from the field of biological science or who has studied biology in high school can easily relate with this concept inheritance and how importance it is.

Without further redo take a look at this example: in a family, the child inherits certain features(attributes) or behavior(method) from the mum or dad say height, complexion, passion, agility, temperament etc...

This inheritance approach happens by default in real life scenario. While in programming it can be default and also be defined. So we can decide to say a child is only permitted to have height and good passion but complexion and agility will be private to the parent.

Inheritance is the mechanism in which a class inherit the attributes and methods of another class. The class which inherits from the other class is referred to as child class while the class whose methods and attributes were inherited is known as base class or parent class.

Looking at this approach inheritance can lead to a tree structure. For example tracing a grandchild to the grandparent takes a tree structure.

In inheritance a new class called subclass, derived or child class is created from a super, existing, parent or base class. In the next article we’ll be looking at polymorphism as this concept is more concerned with classes.

Some of the advantages of inheriting attributes and methods from a super class:

  • the subclass can also have defined attributes and methods

  • the subclass can override the inherited attributes and methods

  • optimize the work of programmers

  • Enhances Do not repeat yourself principle efficiently

  • If there’s need for modification of a method it can be done from one point where it was created in a class and it’ll take effect on all the subclasses making use of the method.

As the inheritance grows it begins to take a tree structure. The further it grows the deeper the tree structure. Hence the need for dependency injection.

Types of Inheritance:

  • Single Inheritance: happens if a child class only inherits a single parent class.

  • Multiple Inheritance: happens when a single child inherits from multiple classes.

  • Multi-Level Inheritance: happens when a class is inherited from an inherited class.

For example:

Car is a parent class

Toyota class is inherited from Car

Camry class is inherited from Toyota

  • Hierarchical Inheritance: different inherited classes (derived class) from the same parent classes.

For example

Car is a parent class

Toyota class is inherited from Car

Mercedes class is inherited from Car

Peugoet class is inherited from Car

Please NOTE: The examples below are just to support the explanation of the inheritance. You could look into the programming language documentation for more information. Thanks!

Simple illustration in Javascript:

Javascript use prototype chaining for inheritance. Prototype can also be referred to as a copy or sample. If properties and methods are traced or linked to another object it's called chaining. In the new versions of the ECMAScript sub class can use the extends keyword to inherit from a parent class

// Super || Parent  || Base class
class Car {
  constructor(name) {
    this.name = name;
  }
  info() {
    return 'Car name: ' + this.name;
  }
}

//using extends keyword to inherit the name attribute and info method from class Car.
// Child || sub || derived class 
class Toyota extends Car {
//child class constructor
constructor(name, model) {

//the super keyword is used to reference the parent class 

    super(name);
    this.carmodel = model;
  }

//child class method accessing the inherited method
  details() {
    return this.info() + ' and model is: ' + this.carmodel;
  }

}
let toyota = new Toyota("Camry", "2021");
console.log(toyota.details());

Output

Car name: Camry and model is: 2021

Internally, extends keyword works using the prototype mechanics. It sets Toyota.prototype.[[Prototype]] to Car.prototype.

Simple illustration in Dart:

// Super || Parent  || Base class
class Car{

// parent method
void info(){
    print("Practicing inheritance in dart! from parent class");
}
}

// Child || sub || derived class 
class Toyota extends Car{

//sub class method
void details (){
print("Practicing inheritance in dart! from child class");
}

}

void main() {

var toyota = new Toyota();

//accesing inherited info method from parent class    
toyota.info();

//accesing details method in child class    
toyota.details();

}

Output Practicing inheritance in dart! from parent class Practicing inheritance in dart! from child class

Simple illustration in PHP:

<?php

class Car{
    public $name;
    public $model;

    public function __construct($name, $model) {
        $this -> name = $name;
        $this -> model= $model;
    }

    public function info() {
        echo "Car Name: {$this->name} and Model is:  {$this->model}";
    }
}

/* Toyota is inherited from Car and is referred to as child class*/
class Toyota extends Car{
// the __construct() and info() is inherited
//child class method
    public function details() {
        echo " <br> I'm the child of Car class";
    }
}

$toyota= new Toyota('Camry', 2021);
$toyota->info();
$toyota->details();

In PHP 8 the above can be written in this format..


<?php

class Car{
    public function __construct(public $name, public $model) {}

    public function info() {
        echo "Car Name: {$this->name} and Model is:  {$this->model}";
    }
}

class Toyota extends Car{
    public function details() {
         echo " <br> I'm the child of Car class"; 
    }
}

$toyota = new Toyota('Camry', 2021);
$toyota->info();
$toyota->details();

?>

Output

Car Name: Camry and Model is: 2021

I\'m the child of Car class

Thank you for reading this article.

Stay tune for the next article on the OOP Series: Polymorphism

Please kindly share with your network and feel free to use the comment section for questions, answers and contributions.

You love this article?? please follow me on hashnode or twitter @alemsbaja to stay updated for more on these OOP series.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!