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…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

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.