Let’s imagine two butterflies with totally different wing colors. Both are butterflies and have characteristics of butterflies, what differs is only the color of their wings. Bringing this scenario to programming, if you had to program each of those butterflies, how would you do? The concept of polymorphism in object oriented programming helps you answer that question.

oop_poly

Polymorphism concept allows you to make the transcription of a particular method inherited from a parent class or superclass. Using the scenario of butterflies, we will explain the concept.

First we have to create the butterfly class, according to the concept of inheritance.

class Butterfly

{

Private $speed;

 

function ColorWing(){

echo ‘- Color Black and Orange’;

}

 

function flapWings($v){

$speed -> $v;

}

 

}

 

The Butterfly class now has two methods: one that displays the butterfly wing color and another that is triggered when they flap their wings and increment or not the variable speed.

Now let’s create two classes that inherit the attributes of the Butterfly class, but each has distinct wing colors from the superclass.

 

 

 

class BlueButterfly extends Butterfly

{

function ColorWing(){

echo ‘- Blue Wing’;

}

}

 

class BlackBlueButterfly extends Butterfly

{

function ColorWing(){

echo ‘- Black and Blue Wing’;

}

}

 

Both classes will inherit the standard methods of the class Butterfly, but ColorWing () method has been rewritten, since each has different color wings. So when the BlueButterfly object and BlackBlueButterfly are instantiated and there is a call to the ColorWing () method, the information that will be displayed will be the one implemented in the subclass. If a third class was created and that only inherit the methods and attributes of the superclass, to instantiate it and call it by ColorWing () method, the information showing on the screen would be the one available on the superclass method. To better understand this scenario let’s see an example:

class NormalButterfly extends Butterfly

{

}

 

 

 

$b1 -> new BlueButterfly()

$b2 -> new BlackBlueButterfly()

$b3 -> new NormalButterfly()

 

$b1 -> ColorWing()

$b2 -> ColorWing ()

$b3 -> ColorWing ()

 

 

The information showing on screen would be:

– Blue Wing

– Black and Blue Wing

– Black and Orange Wing

 

Polymorphism concept helps reuse source code, allowing much of a single code can be reused. It also facilitates the implementation of possible peculiarities of each object having attributes and methods in common.

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.