Mada za sehemu hiiCreating programs with inheritance and method overridingMada 2
- Implementing inheritance
- Implementing overriding parent class methods
Inheritance is a key concept in Object-Oriented Programming that allows a new class to reuse properties and methods from an existing class. Think of it like a child inheriting features from parents — the child gets some characteristics automatically without having to define them again. In programming, this means you can create a general class once, then build more specific classes that automatically include everything from the general class, plus their own unique features.
When working with inheritance, two terms are used:
- Base class (also called parent class): A general class that contains properties and methods intended to be inherited by other classes.
- Derived class (also called child class): A class that inherits properties and methods from a base class. It can also add new features or change how inherited methods work.
For example, if you have a base class called Animal, you can create derived classes like Dog or Cat that inherit from it.
In PHP, the extends keyword is used to create a derived class from a base class. The derived class automatically has access to all public and protected properties and methods of the base class.
Syntax:
class DerivedClassName extends BaseClassName {
// new properties and methods specific to this class
}
Example:
// Base (parent) class
class Animal {
public function makeSound() {
echo "Some generic sound";
}
}
// Derived (child) class
class Dog extends Animal {
public function bark() {
echo "Woof!";
}
}
// Creating an object of Dog
$myDog = new Dog();
$myDog->makeSound(); // Inherited from Animal
$myDog->bark(); // Defined in Dog
In this example, the Dog class inherits the makeSound() method from the Animal class. The $myDog object can use both methods.
Sometimes a child class needs to change the behavior of a method it inherited from its parent. This is called method overriding. The child class defines a method with the same name and parameters as the parent class, but with different behavior.
Example:
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.
When $myDog->speak() is called, the overridden method in the Dog class runs instead of the original one from Animal.
If you want to include the parent class behavior in addition to the new behavior in the child class, you can call the parent's method using parent::methodName().
Example:
class Dog extends Animal {
public function speak() {
parent::speak(); // Calls the parent class method first
echo " The dog also barks.";
}
}
This outputs both the parent class message and the new message.
Let us create a program that demonstrates inheritance with vehicles:
<?php
// Step 1: Create a base class
class Vehicle {
public function start() {
echo "The vehicle is starting...<br>";
}
}
// Step 2: Create child classes that extend Vehicle
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>";
}
}
// Step 3: Create objects and call their methods
$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 the same method name start() behaves differently depending on which class the object belongs to. This is the power of inheritance and method overriding.
In Tanzania, inheritance is commonly used in mobile money systems. For example, a banking application might have a base class MobileMoneyAccount with common features like checkBalance() and deposit(). Specific services like M-Pesa and TigoPesa can be created as derived classes that inherit these features but override the transfer() method to handle each company's specific charges and processing steps. This saves programming time because the basic features are written once in the base class and reused across all mobile money services.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Create base and derived classes with appropriate attributes and methods.
Tathmini ya matokeo
Programs demonstrating the proper use of inheritance are created.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Define base and derived classes using inheritance.
- Use constructors in inheritance hierarchies to initialize base and derived class attributes.
Principles: The student should explain the principle of:
- Code reuse and modularity through inheritance.
Theories: The trainee should explain: The trainee should explain:
- How inheritance facilitates hierarchical relationships in object-oriented programming.
- The importance of constructors in ensuring correct object initialization across inheritance hierarchies.
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 a base class (also called parent class) in PHP inheritance?
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