Inheritance in OOP example with PHP

inheritance
Spread the love

Using Inheritance we can share methods or properties from one class to another class. Or you can say using Inheritance allows a class to reuse the code from another class without duplicating it. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods.

The parent class is known as the base class or superclass and the child class is called a subclass. To utilize inheritance you have to use extends keyword of a subclass. Let’s have an example of inheritance.

class ParentClass
{
public $parentProperty1;
protected $parentProperty2;

public function parentMethod()
{
// TODO : any task 
}
}

class ChildClass extends ParentClass
{
public $childProperty;

public function childMethod()
{
// TODO : any task 
}
}

$ch = new ChildClass();

$ch->parentProperty1 = "Use parent Public property";
$ch->parentProperty2 = "Use parent protected property";
$ch->parentMethod();
$ch->childMethod();

Well, in this example you can see that I define two methods. One is the base class which is denoted (ParentClass) another one is a subclass which I denoted the (ChildClass). In the parent class, I defined two properties and a method. Then I create an instance of the child class. After then I use both class’s properties and methods.

You can use parent classes’ property or method inside the child class. To do that you have to use the $this keyword to access parent classes’ method or properties. Also, You can override the parent classes’ method into the child class.

Example of Method Overridding

class ChildClass extends ParentClass
{
public $childProperty;

public function childMethod()
{
// TODO : any task 
}

public function parentMethod()
{
// Method Override 
}
}

Well, in the child class I re-defined the parentMethod() method again which is known as method override.

So to re-use code you can extends many classes whatever you want. Which is known as multi-level inheritance.

The final keyword

Well, if you want to prevent the inheritance of a class you can use the final keyword of this class. Not only class you can also use the final keyword in a method to prevent an override. Let’s see this example

final class A
{

final public function anyName()
{
# code...
}
}

In this example, I used the final keyword before class A. So you can’t extend this class. Also inside this class the anyName method you can’t override.

Read More : Magic Methods in PHP OOP

Summary

Inheritance allows a class to reuse the code of another class without duplicating it.

Use the extends keyword to define one class that inherits from another class.

A class that inherits another class is called a subclass, a child class, or a derived class. The class from which the subclass inherits is a parent class, a superclass, or a base class.

A subclass can have its own properties and methods.

Use $this keyword to call the methods of the parent class from methods in the child class.


Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m an Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://tutspack.com

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.