Mada za sehemu hiiImplementing encapsulation and abstractionMada 2
- Implementing encapsulation of class attributes and methods
- Implementing abstraction of functionalities using abstract classes and method
Abstract Classes and Methods
Abstraction means focusing on what something does instead of how it does it. It allows you to define a standard structure that can be shared by many things, while letting each of them handle its own details differently.
Think about public transport in Tanzania: buses, taxis, bodaboda, and bajaji all have one thing in common—you must pay for the ride. However, how you pay might differ: cash, mobile money, or bank transfer. You do not need to know how the money is processed behind the scenes; you just need a way to make the payment.
In programming, abstraction lets you define a common method (like makePayment()) and let each transport type implement it in its own way.
An abstract class is a special type of class that provides a general template or blueprint for other classes. It has two important rules:
- Cannot be instantiated directly — you cannot create an object from an abstract class using
new - Defines common features that its child classes will inherit
Some methods in an abstract class may be abstract (without code), and these must be implemented by any class that inherits from it.
Think of it like an "Animal" abstract class. All animals eat and sleep, but you cannot create a generic "Animal" object—you can only create specific animals like Dog or Cat.
An abstract method is a method declared in an abstract class that contains no code. It acts as a rule that says: "Any class that inherits from this abstract class must provide its own version of this method."
The syntax for declaring an abstract method is:
abstract public function methodName();
The child class must implement (provide code for) all abstract methods defined in the parent abstract class.
The following example from the textbook shows how to use abstract classes and methods:
<?php
// Abstract class: cannot be instantiated directly
abstract class Shape {
// Abstract method: must be implemented by child classes
abstract public function calculateArea();
// Regular method: shared by all shapes
public function describe() {
echo "This is a shape.\n";
}
}
// Child class: Circle
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
// Implements the abstract method
public function calculateArea() {
return pi() * $this->radius * $this->radius;
}
}
// Child class: Rectangle
class Rectangle extends Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
// Implements the abstract method
public function calculateArea() {
return $this->width * $this->height;
}
}
// Create objects of specific shapes
$circle = new Circle(5);
$rectangle = new Rectangle(4, 6);
// Call describe() from the abstract class
$circle->describe();
$rectangle->describe();
// Call the implemented calculateArea() methods
echo "Area of Circle: " . $circle->calculateArea() . "\n";
echo "Area of Rectangle: " . $rectangle->calculateArea() . "\n";
?>
Output:
This is a shape.
This is a shape.
Area of Circle: 78.539816339745
Area of Rectangle: 24
-
An abstract class cannot be instantiated directly (using
new Shape()would cause an error). -
An abstract method must be implemented by any subclass that extends the abstract class.
-
Abstract classes can contain both abstract methods (without body) and regular methods (with body).
-
Abstraction provides a uniform structure — all subclasses must follow the same rules.
-
Regular methods in an abstract class are inherited by all subclasses, making code reusable.
<?php
abstract class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
// Abstract method
abstract public function makeSound();
// Regular method
public function describe() {
echo "$this->name is an animal.<br />";
}
}
class Dog extends Animal {
public function makeSound() {
echo "$this->name says Woof!<br />";
}
}
class Cat extends Animal {
public function makeSound() {
echo "$this->name says Meow!<br />";
}
}
$dog = new Dog("Benjy");
$dog->describe();
$dog->makeSound();
$cat = new Cat("Whiskers");
$cat->describe();
$cat->makeSound();
?>
This shows polymorphism: different classes (Dog and Cat) respond differently to the same method (makeSound()).
In Tanzania, mobile money services like M-Pesa and Tigo Pesa use abstraction to process payments. An abstract class called PaymentMethod can define a common method called pay(), while classes like MobileMoney, BankTransfer, and Cash each implement their own way of completing the payment. A farmer in Morogoro selling maize can receive payments through any of these methods without needing to understand the technical details behind each one—the system uses abstraction to handle the different payment processes uniformly.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Define abstract classes and methods with appropriate syntax.
- Implement concrete methods in derived classes, adhering to abstract class requirements.
- Demonstrate the application of abstraction to improve code structure and consistency.
Tathmini ya matokeo
Abstract classes and methods are correctly defined and implemented in derived classes.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Define abstract classes and abstract methods.
- Use derived classes to implement the functionality defined by abstract methods.
Principles: The student should explain the principle of:
- Abstraction to hide unnecessary implementation details and expose essential features.
- Enforcing a consistent interface through abstract classes and methods.
Theories: The trainee should explain: The trainee should explain:
- How abstraction separates "what" from "how" in program design.
- The role of abstract classes in creating extensible and scalable code.
- The importance of adhering to interfaces defined by abstract classes in derived classes
Circumstantial knowledge:
Detailed knowledge about:
- Common challenges in implementing abstraction and how to address them.
- Best practices for designing abstract classes in real-world applications
Swali
What is an abstract class in PHP?
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