Mada za sehemu hiiCreating programs with inheritance and method overridingMada 2
- Implementing inheritance
- Implementing overriding parent class methods
Method Overriding in Object-Oriented Programming
When we create a child class that inherits from a parent class, the child automatically gets all the methods defined in the parent. But sometimes the inherited method does not quite fit the child class — it needs to behave differently. This is where method overriding comes in.
Method overriding lets a child class redefine a method it inherited from its parent class. The child class keeps the same method name and parameters, but provides its own specific implementation. This is one of the most powerful features of object-oriented programming because it allows related classes to share a common interface while behaving differently.
A child class overrides a method when it defines a method with:
- The same name as the parent class method
- The same parameters (if any)
- Different behavior or code inside the method
When you call that method on a child class object, the child's version runs instead of the parent's version.
Simple Example of Method Overriding
Consider a general Animal class with a speak() method. A Dog class inherits from Animal, but dogs do not make generic animal sounds — they bark. The Dog class can override the speak() method:
class Animal {
public function speak() {
echo "The animal makes a sound.";
}
}
class Dog extends Animal {
public function speak() {
echo "The dog barks.";
}
}
$myDog = new Dog();
$myDog->speak(); // Output: The dog barks.
The Dog class kept the method name speak(), but changed what happens when it is called. This is method overriding in action.
Sometimes when you override a method, you want to include the parent's behavior plus some extra code of your own. You can call the parent's original method using the parent::methodName() syntax.
class Dog extends Animal {
public function speak() {
parent::speak(); // Calls the parent class method first
echo " The dog also barks.";
}
}
When this runs, it will print:
The animal makes a sound. The dog also barks.
This is useful when you want to extend the parent's behavior rather than replace it completely.
Let us walk through a complete example using the vehicle concept from the textbook. This demonstrates inheritance, method overriding, and how different objects respond to the same method name.
Step 1: Create the Base Class
class Vehicle {
public function start() {
echo "The vehicle is starting...<br>";
}
}
The Vehicle class has one method called start() that displays a general message.
Step 2: Create Child Classes That Override the Method
class Car extends Vehicle {
public function start() {
echo "The car starts with a key or start button!<br>";
}
}
class Motorcycle extends Vehicle {
public function start() {
echo "The motorcycle starts with a start button or manually!<br>";
}
}
Both Car and Motorcycle inherit from Vehicle, but each overrides the start() method with its own specific behavior.
Step 3: Create and Use 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 that each object calls the same method name start(), but each produces different output. This is the key benefit of method overriding — you can treat different objects through a common interface while each behaves according to its own nature.
- Method overriding only works between a parent class and a child class (inheritance relationship).
- The overriding method must have the same name and parameters as the parent method.
- Use
parent::methodName()to include the parent's behavior inside the child's method. - The decision of which version to call is made at runtime based on the actual object type — this is called polymorphism.
In Tanzania, method overriding is used when building mobile money systems. For example, a banking app might have a base class called Payment with a method called processPayment(). The MobileMoneyPayment and BankTransferPayment classes both inherit from Payment but override processPayment() to handle money differently — one connects to networks like M-Pesa while the other connects to bank APIs. This allows the app to process many types of payments using the same code structure while each payment method works in its own way.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Define and override methods in derived classes.
- Use super or base class references to access parent class functionality.
Tathmini ya matokeo
Parent class attributes and methods are appropriately overridden in derived classes.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Override methods in derived classes.
- Use the super keyword or equivalent to access parent class methods or attributes.
Principles: The student should explain the principle of:
- Method overriding as a means to extend or customize parent class functionality
Theories: The trainee should explain: The trainee should explain:
- How method overriding enables dynamic behaviour in object-oriented programming.
- The impact of method overriding on code reusability.
Circumstantial knowledge:
Detailed knowledge about:
- Common pitfalls in implementing inheritance, such as incorrect overriding or constructor chaining errors.
- Real-world use cases of inheritance, such as modeling organizational structures or system components.
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