Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Complete Tutorials of PHP OOP Inheritance with Example code

In this Tutorial, we are going to learn about Complete Tutorials of PHP OOP Inheritance with Example code. Through this we will learn about the concept about Inheritance.  But first let me give you a brief explanation of what is Object-oriented programming (OOP)?

What is Object-oriented programming (OOP)

Object-oriented programming consists of combining a set of variables (properties) and functions (methods), which are referred to as an object. These things are arranged into classes in which individual items can be combined. OOP can enable you to consider the objects and the many activities in connection with the objects in a program’s code. For more details you can go on my previous article Read More. [Object-oriented programming (OOP) Concept Simplified!]

What is Inheritance?

Inheritance in OOPS

Inheritance is a fundamental idea in object-oriented programming. This idea may be used to define the relationship between two classes. In PHP’s object model, inheritance is supported.

Inheritance is a concept in object-oriented programming that allows a class to use the properties and methods of another class. We frequently encounter situations in which we must create a new class with all of the functionalities of an existing class as well as some additional methods, more akin to an extension to an existing class. In such cases, we can either copy all of the existing class’s properties and methods into the new class to make them available in the new class, or we can simply inherit the old class in the new class.

If we want to develop numerous similar classes, inheritance comes in use. We may group all of the common attributes and methods into a single parent class, which the child classes will inherit.

The extends keyword is used to specify an inherited class.

Some Explanation about the code:-

where Human is the parent class (also known as the base class) and Male and Female is the subclass or child class. The public and protected methods of the parent class are passed down to the child class. Any inherited method may be redefined or overridden by a child class. If not, when used with a child class object, inherited methods will keep their functionality as described in the parent class.

What are the Feature of a class by using ‘extends’ keyword?

Inheritance in oops
  • The idea of a hierarchical classification is supported.
  • Inheritance has three types, single, multiple and multilevel Inheritance.
  • PHP only supports single inheritance, where just one class from a single parent class can be derived.
  • We can simulate multiple inheritance by using interfaces.

What are important points to remember while using inheritance are:

How to Code Inheritance in Java — Beginner's Tutorial in OOP | by Rishi  Sidhu | Towards Data Science
  • Child class can only access and utilize non-private parent-class properties and methods.
  • The child class can also have its own methods that the parent class cannot find.
  • In addition, child class can override and implement a method specified in parent class.

Example:- Let’s add some methods to our Human class and see how we may use them in the Male and Female child classes.

Output:-

Some explanation about the Example:-

As you can see from the code above, we merely inherited the Human class in both children classes, which allowed them to access and use the parent class’s properties and methods.

Why we use The protected Access Modifier in Inheritance?

OOPs: Inheritance and Polymorphism | by Mukesh Chaudhary | Medium

We have learned about the different Access Modifier and how to control access to different class properties and methods. If you don’t know about What is Access Modifier then you can go on given Link. [Complete Tutorials of PHP OOP Access Modifiers with Example code]

When a child class inherits a parent class, non-private properties and methods can only be accessed and reused. However, for properties we should not apply public access modifier, as the properties may also be accessed outside of the class.

We may apply a protected access modifier to allow only the child class to access parent class properties and methods.

If a class is defined as a protected property or method, the properties and methods can only be accessible in the child class that inherit the class. Let’s understand this with a example

Example:-

Output:-

What is Overriding Inherited Methods in PHP?

Java Method Overriding - CSVeda

In the same manner that the child class can have its own properties and methods, it may override the properties and methods of the parent class. When we override the class’s properties and methods, we rewrite a method or property that exists in the parent again in the child, but give to it a new value or code.

How about a children’s class using a parent-class procedure, but somewhat different? It can achieve this by overriding the method specified by the parent class and defining itself. This is called overriding method.

Let’s understand this with a example:-

Output:-

Some explanation about the Example:-

In the code above we have a parent class called Vehicle and two child classes namely Car and Motorcycle which extend the parent class. We have a drive() function in the parent class, which we have overridden and given a distinct definition in our child classes.

How to override the parent’s properties and methods in the child class?

Access parent's overridden method from parent's context in PHP - Stack  Overflow

Just as the child class can have its own properties and methods, it may also override parent class properties and methods. When we override the properties and methods of the class, we rewrite a method or property that again exists in the child’s parent, but assign it a different code or value.

Let’s understand this with a example:-

We construct a hello() in the parent class in the following example, which returns the string “beep” and overrides it in the child class with a method that produces a different string, “Halllo,” using that same name method.

Output:-

Some explanation about the Example:-

The result represents the overriding by the child method of the same name of the hello() of the parent class.

How to prevent the child class from overriding the parent’s methods?

Method Overriding in Scala - GeeksforGeeks

In order to prevent the method that overrides the methods of the parent class in the child class, the method can be prefixed in the parent with the Final keyword.

Let’s understand this with a example:-

We define the hello() function as final in the parent class in the example shown below, but try nevertheless to override it in the child class. What do you think may happen if we try to override a defined final method?

Output:-

Some explanation about the Example:-

Since the hi method has been defined to be the parent class final, in the child class we cannot override it.

Why Inheritance is Important?

Inheritance in object-oriented programming is an immensely helpful concept which enables us to avoid code duplication and manage major task simply.

All the public and protected properties and methods of the parent class are inherited by the child class. It can also have its own characteristics and approaches.

Inheritance is quite helpful if we are going to build numerous similar classes. In the same parent class we may insert the common properties or methods and then inherit them from the child classes.

Conclusion

We use Inheritance to decrease the duplication of code by using parent class code in child classes. In this article, we learnt the principle of Inheritance, which is one of the foundations of object-oriented programming. We use Inheritance to decrease the duplication of code by using parent class code in child classes. Hope this will help you to understand the concept of PHP OOP Inheritance. Thank you