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…

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...

Disabling autofill and autocomplete Chrome browser in login screens

Hello, here I want to show an alternative to disable the autofill and autocomplete of the chrome bro...

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.