Inheritance is one of the core concepts of Object-Oriented Programming (OOP). In PHP, inheritance allows you to create a new class based on an existing class. The new class inherits properties and methods from the parent class.
Parent Class (or Base Class or Superclass): The class whose properties and methods are inherited by another class.
Child Class (or Subclass or Derived Class): The class that inherits properties and methods from another class.
To use inheritance in PHP, you use the `extends` keyword.
PHP supports single inheritance only, meaning a class can inherit from only one class.
Let's create an example to understand inheritance better:
// Parent class
class Animal {
public $name;
function __construct($name) {
$this->name = $name;
}
public function speak() {
echo "I am an animal.";
}
}
// Child class
class Dog extends Animal {
public function speak() {
echo "Woof! Woof!";
}
public function fetch() {
echo "{$this->name} is fetching the ball.";
}
}
// Using the classes
$dog = new Dog("Buddy");
$dog->speak(); // Outputs: Woof! Woof!
$dog->fetch(); // Outputs: Buddy is fetching the ball.
5.1 Overriding: You can redefine a method in a child class. This is called overriding. In the above example, the speak method in the Dog class overrides the speak method in the Animal class.
5.2 parent Keyword: If you want to call a parent class's method within a child class, you can use the parent keyword.
Example:
class Cat extends Animal {
public function speak() {
echo "Meow! ";
parent::speak(); // Call the parent class's speak method
}
}
$cat = new Cat("Whiskers");
$cat->speak(); // Outputs: Meow! I am an animal.
5.3 Access Control:
Remember that private properties and methods of the parent class are not accessible in the child class. Protected properties and methods, on the other hand, are accessible in the child class.
Example:
class Bird extends Animal {
public function describe() {
// This will generate an error if $name in Animal is private
echo "This is a bird named {$this->name}.";
}
}
5.4 `final` Keyword:
If you don't want a child class to override a parent class's method, you can declare the parent's method as `final`.
Example:
class Lion extends Animal {
final public function roar() {
echo "Roarrr!";
}
}
class Cub extends Lion {
// This will generate an error because the roar method in Lion is final
public function roar() {
echo "Tiny Roar!";
}
}
Inheritance is a powerful feature in OOP that allows for code reuse and the creation of hierarchical relationships between classes. When used wisely, it can make your code cleaner and more maintainable.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here