Abstract classes are the classes in which at least one method is abstract. Unlike C++ abstract classes in PHP are declared with the help of abstract keyword. Use of abstract classes are that all base classes implementing this class should give implementation of abstract methods declared in parent class. An abstract class can contain abstract as well as non abstract methods.
Following are some important points about abstract class and method.
1 - An abstract class can have methods and properties just like any other normal class.
2 - An abstract class cannot be instantiated, hence we need to create a child class which extends it, then we can create object of the child class.
3 - If a class has even a single abstract method then the class should also be abstract.
4 - An abstract method is just the declaration, where we provide name of the method and argument, while the body part is empty.
Don't worry if its too much for you to understand. We will cover all the points step by step with examples, lets start by understanding how we create an abstract class.
Creating an abstract Class
To declare a class abstract, we need to use the abstract keyword before the name of the class.
Example -
<?php
// abstract class
abstract class Vehicle {
// abstract function mileage
abstract public function mileage() {
}
}
?>
In the example above, our class Vehicle is an abstract class, which has an abstract method.
The idea behind creating abstract class is to bound developers to follow a set of guidelines, for example, if you want to create a new class which extends our class Vehicle then you will have to provide definition for the abstract method mileage(), else the child class should also be abstract. Hence, it is mandatory for all the child classes to provide definition for the method mileage().
Non Abstract Method in Abstract Class
Any class with even a single abstract method must be declared abstract. But an abstract class can have non-abstract methods as well, which can be accessed and used directly by the child classes, without overriding them.
Let's extend the example above, and include a non-abstract method in our class Vehicle.
<?php
// abstract class
abstract class Vehicle {
// protected variable
protected $name;
// non-abstract public function start
public function start() {
echo $this->name. " - Engine start...<br/>";
}
// non-abstract public function stop
public function stop() {
echo $this->name. " - Engine stop...<br/>";
}
// non-abstract public function setName
public function setName($name) {
$this->name = $name;
}
// abstract function mileage
abstract public function mileage() {
}
}
?>
In the code above, we have added three non-abstract methods namely start(), stop() and setName() to our abstract Vehicle class.
Inheriting Abstract Classes
Just like any other class, we can create classes extending abstract classes too.
The only difference here is that the child class must provide definition for the abstract method declared in the abstract parent class.
If the child class doesn't provide definition for the abstract method, then it should also be defined as an abstract class.
Let's create two child classes inheriting the class Vehicle and which will have definition for the abstract method mileage():
<?php
// child class
class Car extends Vehicle {
public function mileage() {
echo "I am " . $this->name . "<br/>";
echo "My mileage range is - 15 to 22 Km/L";
}
}
?>
We can have as many child classes as we want, lets have another class.
<?php
// child class
class Motorcycle extends Vehicle {
public function mileage() {
echo "I am " . $this->name . "<br/>";
echo "My mileage range is - 35 to 47 Km/L";
}
}
?>
As mentioned above that an abstract class cannot have any object, once we have proper child classes defined, we can create object for them.
<?php
$car = new Car();
$car->setName("BMW X1");
$car->mileage();
?>
Output -
I am BMW X1
My mileage range is - 15 to 22 Km/L
No comments:
Post a Comment