PHP Core / OOPs / Inheritance
Inheritance
-
Note
Inheritance is one of the core principles of Object-Oriented Programming (OOP) that allows a class (subclass or derived class) to inherit attributes and methods from another class (superclass or base class).
It help us to avoid code duplication Code Reusability:Inheritance promotes code reuse by allowing a subclass to inherit attributes and methods from its superclass. Common functionalities implemented in the base class can be reused by multiple subclasses, reducing redundancy and making the code more modular.
ExtensibilityInheritance supports the extension of existing classes. New functionality can be added to a subclass without modifying the code of the superclass. This makes it easier to introduce new features to a system without affecting the existing code.
Maintenance and Updates:Inheritance simplifies maintenance and updates. Changes made to the superclass automatically apply to all its subclasses, ensuring consistency and reducing the likelihood of errors. This streamlines the process of making updates or fixing issues across related classes.
When use
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the member of the existing class.
types of inheritance