Inheritance in php

In the last lesson we have learnt about Magic methods and constant . Today we will learn about inheritance. So let’s start with the question what is inheritance and their benefits and uses.

By definition : In object oriented programming Inheritance is a concept which is used to achieve the public methods and properties from another class(i.e child class).

Inheritance by real life scenario : 

The relationship of a father and child. Son gets the property of the father. similarly when we will be parent then our child will access our property and this process goes on…

Types of inheritance :

In php there are they types of inheritance i.e

  • single Inheritance
  • multilevel inheritance
  • multiple inheritance

However single and multilevel inheritance is supported by PHP and multiple inheritance is achieved by some jugad technique (hack). 

Single Inheritance  : In single inheritance, there is only two class one is parent class and other is child class where child class inherits methods and properties of its parent class.

Multilevel Inheritance  : In Multilevel inheritance, there is a Hierarchical inheritance, it means the grand parent class is inherited by parent class and the parent class is inherited by child class.

 

Benefits of using inheritance : Inheritance gives us code reusablity features. It means we can use a block of code many times without writing the same code multiple times (code duplication).

Suppose we have five login pages in our website. So their are two ways to create five login script. 

First one is to write the same login script on the five pages (Which is code replication i.e not best practice)

Second way is to inherit the login script on five pages. (Which is using the code by inheriting the class name. It also save code replication on multiple pages.).

Inheritance gives the facility to write a code in parent class and use it in child as well as in parent class itself.

Now let’s take an example to understand this inheritance.

Parent Class

 

Child Class

Till now we have seen what is inheritance and now we will see how to write syntax to inherit parent class property and methods from child class. Even we can create multiple child class and inherit a single parent class. (Real life example : It is also possible that a father has more than one child and all the child have equal rights to access his father property).

When we need to access parent class property or methods then we use syntax like this :

class Son extends Father{
// Here parent class name is Father and Child class name is Son
}

Now lets write a program which combining both parent and child class relationship and we will try to access the parent property and methods from it.

Output :
Sharma
$20000

To access a parent class we use keyword extends which gives the access rights to parent class method and properties which is declared as public.

Restriction : 

We can’t access the private methods and properties of parent class from a child class. (This is same as we can’t asks for parents bank account password which he wants to keep secret.)

What happens when we try to access the private methods and properties of parent class.

Output :Fatal error: Call to private method Fruit::getName(),

Hence now it is clear that we can’t access the private properties and methods of parent class.

Does a child class can have its own methods and properties ?

Yes, Why not like any class child class can also have its own methods and properties and a child class can use them from both parent class and within itself. Here is an example.

Output : 

Banana
we will now access the methods and properties from child class
Yellow
sweet

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

We can override the parent class properties and method from the child class by using the same method name or property name in the child class but the values must be different which are returned from both the classes. Below is an example of overriding a method and property of parent class.

Output :
Welcome from child
Hello from child

How to prevent a child class, overriding its parent class property and methods ?

Sometimes while programming by mistake we use same method and property name in both parent and child class. Which cause child class override its parent methods and properties which we don’t want.

A child class can be prevented to override a parent class by using final keyword as prefix in parent class method and properties.

Output : Fatal error: Cannot override final method Welcome::sayHello() ,

 

In this lesson we learnt about Inheritance and its benefits, overriding parent class methods and properties, final keyword. In the next lesson we will learn about abstract methods and classes.

Leave a Reply

Your email address will not be published. Required fields are marked *