PHP OOP – Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. The class that inherits properties and methods is called the child class or subclass, while the class that provides the properties and methods is called the parent class or superclass.

In PHP, we can define inheritance using the extends keyword. The child class can inherit all the properties and methods of the parent class, and can also override the properties and methods of the parent class with its own implementation.

Here is an example of inheritance in PHP:

class Vehicle {
    public $color;

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

    public function drive() {
        echo "The vehicle is driving";
    }
}

class Car extends Vehicle {
    public $brand;

    public function __construct($color, $brand) {
        parent::__construct($color);
        $this->brand = $brand;
    }

    public function drive() {
        echo "The car is driving";
    }

    public function honk() {
        echo "Beep beep";
    }
}

$car = new Car("red", "Toyota");
echo $car->color; // Output: red
echo $car->brand; // Output: Toyota
$car->drive(); // Output: The car is driving
$car->honk(); // Output: Beep beep

In the example above, we have defined a Vehicle class with a constructor and a drive() method. We then define a Car class that extends the Vehicle class and adds its own honk() method. The Car class overrides the drive() method from the Vehicle class with its own implementation.

In PHP, there are four types of inheritance:

  1. Single inheritance
  2. Multilevel inheritance
  3. Hierarchical inheritance
  4. Multiple inheritance

Single inheritance

Single inheritance in PHP is when a child class inherits properties and methods from a single parent class. To implement single inheritance, the keyword “extends” is used.

The main features of single inheritance in PHP are:

  1. It allows a child class to inherit properties and methods from a single parent class.
  2. It promotes code reusability and helps in avoiding duplicate code.
  3. It allows the child class to override methods and properties of the parent class with its own implementation.
  4. It provides a way to organize and structure code in a hierarchical manner.
  5. It allows multiple instances of the child class to be created, each with their own properties and behavior inherited from the parent class.
  6. It allows for better maintainability and extensibility of code.

Here’s an example:

class ParentClass {
   public function parentMethod() {
      echo "This is a parent method.";
   }
}

class ChildClass extends ParentClass {
   public function childMethod() {
      echo "This is a child method.";
   }
}

$obj = new ChildClass();
$obj->parentMethod(); // This is a parent method.
$obj->childMethod(); // This is a child method.

In this example, ChildClass extends ParentClass, which means that it inherits the parentMethod() method from ParentClass. The childMethod() method is defined in ChildClass and is not inherited from ParentClass. The object $obj of ChildClass can call both the inherited parentMethod() and the defined childMethod().

Multilevel inheritance

Multilevel inheritance in PHP is a type of inheritance in which a derived class inherits properties and methods from a parent class, which in turn is derived from another parent class. In this type of inheritance, the derived class can access both the properties and methods of its immediate parent class as well as the properties and methods of its parent’s parent class.

Here is an example of multilevel inheritance in PHP:

class Vehicle {
  protected $num_wheels;
  protected $fuel_type;

  public function __construct($num_wheels, $fuel_type) {
    $this->num_wheels = $num_wheels;
    $this->fuel_type = $fuel_type;
  }

  public function getInfo() {
    echo "This vehicle has " . $this->num_wheels . " wheels and runs on " . $this->fuel_type . ".";
  }
}

class Car extends Vehicle {
  private $num_doors;

  public function __construct($num_wheels, $fuel_type, $num_doors) {
    parent::__construct($num_wheels, $fuel_type);
    $this->num_doors = $num_doors;
  }

  public function getInfo() {
    echo "This car has " . $this->num_wheels . " wheels and " . $this->num_doors . " doors, and runs on " . $this->fuel_type . ".";
  }
}

class SportsCar extends Car {
  private $top_speed;

  public function __construct($num_wheels, $fuel_type, $num_doors, $top_speed) {
    parent::__construct($num_wheels, $fuel_type, $num_doors);
    $this->top_speed = $top_speed;
  }

  public function getInfo() {
    echo "This sports car has " . $this->num_wheels . " wheels and " . $this->num_doors . " doors, runs on " . $this->fuel_type . ", and has a top speed of " . $this->top_speed . ".";
  }
}

$sports_car = new SportsCar(4, 'gasoline', 2, '250 mph');
$sports_car->getInfo();

In this example, the Vehicle class is the parent class of the Car class, which is the parent class of the SportsCar class. The SportsCar class inherits properties and methods from both the Car and Vehicle classes, and it can override methods defined in both of these classes if necessary.

Hierarchical inheritance

Hierarchical inheritance is a type of inheritance in PHP OOP where a single base class serves as a parent class for multiple derived classes. In this type of inheritance, one class is inherited by multiple classes, forming a hierarchy or tree-like structure. Each child class inherits all the properties and methods of the parent class and can have additional properties and methods of its own.

For example, consider a base class called “Animal” that has some common properties and methods such as “name”, “age”, “eat()”, and “sleep()”. Now, we can create multiple derived classes such as “Dog”, “Cat”, and “Bird” that inherit from the “Animal” class. Each derived class can have additional properties and methods specific to it, such as “breed” for a dog, “furColor” for a cat, and “wingspan” for a bird.

Here’s an example of hierarchical inheritance in PHP:

class Animal {
   protected $name;
   protected $age;

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

   public function eat() {
      echo $this->name . " is eating.<br>";
   }

   public function sleep() {
      echo $this->name . " is sleeping.<br>";
   }
}

class Dog extends Animal {
   private $breed;

   public function __construct($name, $age, $breed) {
      parent::__construct($name, $age);
      $this->breed = $breed;
   }

   public function bark() {
      echo $this->name . " is barking.<br>";
   }
}

class Cat extends Animal {
   private $furColor;

   public function __construct($name, $age, $furColor) {
      parent::__construct($name, $age);
      $this->furColor = $furColor;
   }

   public function meow() {
      echo $this->name . " is meowing.<br>";
   }
}

// create objects of derived classes
$dog = new Dog("Rufus", 3, "Labrador");
$cat = new Cat("Smokey", 2, "Gray");

// call methods of base and derived classes
$dog->eat();
$cat->sleep();
$dog->bark();
$cat->meow();

In this example, the “Animal” class is the base class and the “Dog” and “Cat” classes are the derived classes. The “Dog” and “Cat” classes inherit the properties and methods of the “Animal” class and also have additional properties and methods specific to them. The constructor of each derived class calls the constructor of the parent class using the parent::__construct() function to set the common properties.

Multiple inheritance

PHP does not support multiple inheritance directly, but it can be achieved using interfaces. An interface defines a contract that a class can implement, and a class can implement multiple interfaces. By implementing multiple interfaces, a class can achieve the functionality of multiple inheritance.

For example, consider the following code:

interface Interface1 {
   public function method1();
}

interface Interface2 {
   public function method2();
}

class MyClass implements Interface1, Interface2 {
   public function method1() {
      // Method implementation
   }

   public function method2() {
      // Method implementation
   }
}

Here, the MyClass class implements both Interface1 and Interface2, achieving multiple inheritance through interfaces. The MyClass class must provide implementations for all the methods defined in both interfaces.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial