Introduction to Inheritance:
A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones. Similarly, inheritance is a feature of OOP, which allows making use of an existing class without making changes to it. This can be achieved by deriving a new class (Derived class) from the existing class (Base class). More properties can be added to the derived class if needed. Therefore, the complexity of derived class may grow as the level of inheritance grows.
“The mechanism of driving a new class from the older one is called Inheritance. The old class is called the Base class or Super Class or Parent Class and the new one is called the Subclass or Derived Class or Child Class.”
For example, the Programmer is a class in itself. It is also a member of an Employee class as shown below:
OOP provides several benefits to the programmer. The principal advantages are:
- We can eliminate redundant code.
- We can extend the use of existing classes.
- It is easy to partition the work in the form of objects.
- Complexity can be easily managed.
- We can build programs using standard working modules that communicate with one another. This leads to the saving of our development time and higher productivity.
- It is possible to map multiple objects in the program.
- The principle of data hiding helps the programmer to build a secure program that can’t be invaded by code in other parts of the program.
- Message passing techniques are used for passing the data between two objects.
Access Control provides a restriction for accessing the members of a class. It determines how a member can be accessed. The following access specifiers provided by PHP are:
- Public Access
- Private Access
- Protected Access
A variable or method declared as the public has the widest possible visibility and accessible everywhere. The Public member of a class is visible or accessible to its own class, its derived classes and outside the class.
The Private members of the class can be accessed within their own class. They cannot be inherited by subclasses and are therefore inaccessible in subclasses.
Class members are declared as protected when they can be accessed only within the class itself and by inheriting classes.
Types of Inheritance:
- Multiple (Not Supported in PHP)
- Hybrid (Not Supported in PHP)
When one class extends the properties of Another class then we call it a single inheritance. In the below example, A is a parent class of B and B would be a child class of A.
In this , a class can inherit the properties of more than one parent class. For example; A, B are parent classes of C and C would be a child class of A and B.
PHP doesn’t support multiple inheritances but using Traits in PHP instead of classes, we can implement it.
When multiple classes inherit the properties of the same class then it is called hierarchical inheritance. In this kind of inheritance one class is inherited by multiple subclasses. In below example class B, C and D inherit the same class A. A is parent class (or base class) of B, C & D.
In this inheritance, We derived new classes at different levels. When a class is derived from a class which is also derived from another one, i.e. a class having more than one parent class, such inheritance is called Multilevel Inheritance. As shown in the flow diagram C is a subclass or child class of B and B is a child class of A.
Hybrid inheritance is a combination of multilevel and multiple inheritance. This can be achieved by combining more than one type of inheritance. A hybrid inheritance can be achieved in PHP in the same way as multiple inheritances. Using Traits one can have multiple as well as a hybrid inheritance in PHP.
PHP doesn’t support Hybrid type but using Traits in PHP instead of classes, we can implement it.
PHP programming is a valuable skill that a programmer can have. Let StudySection help you out with proving your programming skills through its PHP certification. StudySection provides beginner as well as expert-level certifications in PHP programming to prove your level of skills in PHP programming.