It is common to identify situations where an object can have more than one common feature. For example, a beer. Our example may seem unusual, but it will help us for illustration and understanding. Before actually be a beer, it is a drink and can also be a recipe ingredient. Many recipes take beers and that it can also be classified as an ingredient. Analyzing this scenario is that we will explain following the concept of multiple inheritance in object-oriented programming.

Multiple inheritance in OOP

A beer has specific characteristics such as white collar and a yellowish or darker color. It is also a drink which in many cases must be stored in a special way. She is also a type of ingredient, since in cuisine it is very common to use the beer in dishes that take meat and special sauces.

Bringing this scenario for the programming codes, how could a “beer” class have drink attributes and ingredient attributes at the same time? To solve this problem, you must then apply the concept of multiple inheritance. Check out the following example:

class Drink{

$type;

public function setType($var)    {

$type = $var;

}

}

 

class Ingredient{

$quantity;

$recipename;

public function setQuantity ($qt)             {

$ qtde = $qt;

}

 

public function setRecipeName($name)               {

$ recipename = $name;

}

}

 

class Beer extends Beer, Ingredient{

}

 

$beer = new Beer;

$beer. setTypo(“Pilsen”);

$beer. setRecipeName(“Beer Sauce”);

$beer. setQuantity (“2 cups”);

 

The syntax used to apply the class inheritance is also used to assign one or more classes, separated by commas. The behavior is also the same, from the descendant class it is possible to call the parent class objects. In the example, the beer type is indicated using the setType () method. On the other hand, the amount that will be used in the indicated recipe, is set using the setQuantity () method. Every mentioned method was implemented in a different class.

This dynamism in the use of the concept of heritage optimizes production, ensures the ultimate in code reuse and centralizes the control of the information, as a generic class can be used in several classes descendants.

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.