PHP
Summary - Inheritance
Build an animal class hierarchy with extends, parent:: and override.
Summary - Inheritance
In this group you learned inheritance. You now know five core concepts:
- Inheriting from a class with
extends - Accessing the parent with
parent:: - Changing behavior with method override
- Child-friendly access with
protected - Building multi-level inheritance chains
Together, these concepts form the foundation of class hierarchies. They reduce code duplication, enable extensible designs and let you model real-world "type" relationships.
In the final task you'll combine all of them in an animal hierarchy: Animal, Dog and Puppy classes.
Exercise Animal → Dog → Puppy hierarchy
In the final task you'll combine everything! The Animal class is provided; it has a name property and a sound() method (returns "No sound"). You will: derive Dog from Animal and override sound() to return "Woof woof!". Derive Puppy from Dog and override sound() to return parent::sound() . " (mini)". Create a Puppy instance named Buddy and print its name and sound: echo $puppy->name . ": " . $puppy->sound();
Show example solution
:sound returns 'Woof woof!'; Puppy::sound calls parent::sound() and appends ' (mini)'.
Example solution:
<?php
// Animal class
class Animal {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function sound(): string {
return "No sound";
}
}
// Dog class
class Dog extends Animal {
public function sound(): string {
return "Woof woof!";
}
}
// Puppy class
class Puppy extends Dog {
public function sound(): string {
return parent::sound() . " (mini)";
}
}
$puppy = new Puppy("Buddy");
echo $puppy->name . ": " . $puppy->sound();
You finished the inheritance group
Congratulations! You finished the inheritance group. You can now build extensible class hierarchies that avoid code duplication, using extends, parent::, override and protected.
In the next group you'll meet abstract classes and interfaces. These structures will strengthen inheritance further and let you define shared templates that other classes must follow.
Take a short break to consolidate what you learned; then we'll continue together.
Test yourself
-
Which of the following is wrong?
- Inheritance is done with extends
- The parent is accessed with parent::
- A child class cannot access private members
- When the child defines a constructor, the parent's is called automatically
Show answer
When the child defines a constructor, the parent's is called automatically — Correct; this statement is wrong. The parent's constructor is not called automatically; parent::__construct() is required.
-
Complete Puppy's override
public function sound(): string {
return ____::sound() . " (mini)";
}
Show answer
public function sound(): string {
return parent::sound() . " (mini)";
}
-
Match inheritance concepts
- extends
- Inheritance keyword
- parent::
- Parent access
- override
- Redefine with same name
- protected
- Child-friendly access
Next lesson: Project: Utility library