Mada za sehemu hiiImplementing polymorphism with interfaces and abstract classesMada 3
- Designing abstract classes for common behaviour
- Implementing polymorphism with abstract classes or interfaces
- Performing overriding methods to implement polymorphic behaviour
Method Overriding and Polymorphic Behaviour
When we work with inheritance, sometimes a child class needs to change how a method works that it inherited from its parent. This is called method overriding — the child class redefines a method from the parent class to provide its own specific behaviour. Together with inheritance, method overriding enables polymorphism, a powerful OOP concept that allows different objects to be treated the same way while behaving differently.
Method overriding occurs when a derived (child) class defines a method with the same name and parameters as a method in its parent class, but with different behaviour inside the method body. The child class replaces the parent's version with its own.
Why Override a Method?
- To change or extend the behaviour inherited from the parent
- To provide a more specific implementation for the child class
- To ensure each class in a hierarchy behaves appropriately for its type
How to Override a Method
In PHP, you simply redefine the method in the child class using the same name and parameter list. The extends keyword establishes inheritance, and overriding works automatically when the method is called on a child class object.
// Parent class
class Animal {
public function speak() {
echo "The animal makes a sound.";
}
}
// Child class overrides the speak() method
class Dog extends Animal {
public function speak() {
echo "The dog barks.";
}
}
$myDog = new Dog();
$myDog->speak(); // Outputs: The dog barks.
When $myDog->speak() is called, the overridden version in Dog runs, not the original from Animal.
Sometimes you want the child class to include the parent's behaviour plus its own. Use parent::methodName() to call the original method from within the overridden method.
class Dog extends Animal {
public function speak() {
parent::speak(); // Calls the parent's speak() first
echo " The dog also barks.";
}
}
$myDog = new Dog();
$myDog->speak();
// Output: The animal makes a sound. The dog also barks.
This approach lets you reuse parent logic while extending it.
This example from the textbook demonstrates method overriding with polymorphism. Three classes share the same method name start(), but each produces different output.
<?php
// Base (parent) class
class Vehicle {
public function start() {
echo "The vehicle is starting...<br>";
}
}
// Car overrides the start method
class Car extends Vehicle {
public function start() {
echo "The car starts with a key or start button!<br>";
}
}
// Motorcycle overrides the start method
class Motorcycle extends Vehicle {
public function start() {
echo "The motorcycle starts with a start button or manually!<br>";
}
}
// Creating and using objects
$genericVehicle = new Vehicle();
$myCar = new Car();
$myMotorcycle = new Motorcycle();
$genericVehicle->start();
$myCar->start();
$myMotorcycle->start();
?>
Output:
The vehicle is starting...
The car starts with a key or start button!
The motorcycle starts with a start button or manually!
Notice how each object uses the same method name start(), but the behaviour differs depending on the object's class. This is polymorphism in action.
Polymorphism (meaning "many forms") allows objects of different classes to be treated as objects of a common type. When a parent class defines a method, and child classes override it, you can work with all of them using the same method name.
In the vehicle example, you could write a function that accepts any Vehicle object:
function startVehicle(Vehicle $v) {
$v->start();
}
startVehicle(new Car()); // The car starts with a key...
startVehicle(new Motorcycle()); // The motorcycle starts with...
The function does not need to know which specific type of vehicle it receives — polymorphism ensures the correct start() method runs for each.
- Use the
extendskeyword to create a child class that inherits from a parent - Override a method by defining it with the same name in the child class
- Use
parent::methodName()to call the parent's version from within the child - Polymorphism lets different objects respond to the same method call in different ways
- Method overriding supports dynamic dispatch — the correct method is chosen at runtime based on the actual object type
In Tanzanian mobile money systems like M-Pesa or Tigo Pesa, polymorphism is used when processing different types of transactions. For example, a MobileMoney system might have a parent class Transaction with a method process(), overridden by AirtimePurchase, SendMoney, and CashWithdrawal classes. Each transaction type processes differently, but the system can handle all of them uniformly through the same process() method, making the code flexible and easy to extend as new features are added.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Override methods in derived classes adhering to the parent class or interface contracts.
- Demonstrate the consistent behavior of overridden methods across different derived classes.
Tathmini ya matokeo
Overridden methods are correctly implemented to demonstrate polymorphic behavior implementation.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Override methods in derived classes while maintaining consistency with parent class
Principles: The student should explain the principle of:
- Method overriding to implement polymorphic behaviour.
- Encapsulation and inheritance as enablers of polymorphism.
Theories: The trainee should explain: The trainee should explain:
- How overriding enhances flexibility and customizability.
- The impact of polymorphism on reducing code duplication.
Circumstantial knowledge:
Detailed knowledge about:
- Common challenges and pitfalls in method overriding and their solutions.
- Best practices for designing and implementing polymorphic behaviour.
Swali
What is method overriding in object-oriented programming?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado