In biology, every mammal has specific features that ultimately classify it in this large group. But every mammal has its own peculiarities – some fly, others swim, some have four legs and others two legs. Analyzing this context, you will now find it easier to understand how is the concept of inheritance in object-oriented programming.

poo_Inheritance

In OOP, inheritance is done through a superclass that provides its subclasses a number of attributes and methods already ready. For example, the class “doberman” can inherit the superclass “dog” the method bark (), for example. In the same context, the “rottweiler” class can also inherit this same method, but the other attributes as fell and weight will be different.

Let us understand in practice how to work with this concept:

class Dog{

public $weight;

public $fell;

function Bark{

echo “woof woof”;

}

}

 

class Doberman extends Dog{

$weight = “97lbs”;

$fell = “short”;

}

 

class Rottweiler extends Dog{

$weight = “121lbs”;

$fell = “medium”;

}

 

 

 

In the block of code shown in the example, the Doberman and Rottweiler classes inherit the attributes and methods of the superclass Dog. The bark () method is the same for both, but the value of the attributes have changed.

You can access public attributes and methods of the superclass only instantiating objects.

$dob = new Doberman;

$rot = new Rottweiler;

$dob->Bark()

$rot->Bark()

 

Note that there is a great reuse of the generated source code, so that common methods to both classes did not need to be written again. The concept of inheritance is useful precisely at this point.

On a daily basis, the programmer can then create superclasses that will serve as a sort of “template” for subclasses, writing only once a method that can be reused in many others. Remember that a superclass can be used by several subclasses, simply by performing the indication of the superclass using the command extends and, soon after, the name of the superclass.

Source code reuse is indispensable for any productive development environment. Therefore, to understand the concept of heritage and apply it is essential to ensure greater productivity.

You might also like…

Scriptcase uses the MVC template?

Maybe you might have thought about this at some point, “Scriptcase uses the MVC templates?" Well, ...

How to implement A2F using Own Development in NodeJS in a Scriptcase application

A2F using Own Development in NodeJS A2F: In a previous article, we talked about Swivel as an extern...

Specialists or generalists, who gets a better advantage by using Scriptcase?

Hey there! Today  we will figure what are the ideal profiles for those who wish to take advantage o...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.