Polymorphism

Polymorphism

Polymorphism is a word that sounds complicated; however, really it is not as wild as it first appears.
When we look at the words etymology (history), we see Greek roots for ‘poly’ and ‘morph’.
The direct translations is ‘many forms’, and ‘ism’ means having. So, having many forms.

Do you remember a while back in our inheritance class when we made different types of animals?
Specially let’s combine this with what we learned in the abstraction class.
How you can have a function defined virtually which allows an inheriting class to override it if it wanted.

For instance,

perhaps in our ‘Cat’ class we had listed a speak function.
When a Cat speaks, a cat says Meow.
When a Lion speaks, a lion says Roar

However, remember that a Lion is a Cat.
So, inheritance can drive one form of polymorphism.

That said, we will soon discover that there are many forms of polymorphism.
Haha, you might say that the topic of polymorphism is polymorphic itself!


These are both Cats,
but the base form of a cat Meows,
whereas the Lion form Roars.

As we continue with this topic our pairing focus will continue with Emotions.
Emotions are a prime example of polymorphism as well.

All of our emotions would extend from the base definition of emotion.
That means that an emotion has ‘many forms’ such as Happy, Sad, Fear, Love, Anger, Joy, etc.
This is one form of polymorphism.


In a similar sense,
sometimes different functions may be overloaded and behave differently when we feel specific emotions. Just like the overridden Speak() function, different emotions would have their own versions Feel() and Describe() as we discussed previously.
This is also polymorphism.


The two example are called ‘run-time’ polymorphism.
When we overrode the base class’s version of Speak() to produce the Lion’s version we were using run-time polymorphism. Similarly so, overrode definitions in the Happy Emotion, such as Describe() we were using run-time polymorphism.

The other main form of polymorphism In C# is called ‘compile-time’ polymorphism.
These forms are translated during compilation into machine code; however, prior to that they had the same name, but many forms.

Previously we defined an IEmotion interface like this,

Let’s make a person class, which will have an interaction function.

What makes up an interaction?
Are all interactions the same?
Do we accept more input in some interactions than others?
Do we return more outputs in some interactions than others?

Complicated stuff, eh?     
Would you believe that polymorphism allows a computer to process this type of understanding!

In this example we find ourselves with an IPerson interface defining the multiple forms of the Interact(…) method.

There is just one Interact method that we can call; however, depending on the input provided a different form of the method would be ran.

In this example we maybe have an Experience produced from Interact(…) which accepted both a reflected empathyEmotion, and an ICognitive component linguisticThough (language type) as input parameters.

However, we may also have an interaction where we had blocked the empathic input, and only accepting some ICognitive input. If that was the case, a different version of Interact(…) would be processed.

Finally, we may also have a type of Interact(…) where no ICognitive component was exchanged, only empathetic IEmotion reflection. In this case the third form of Interact(…) would be processed.

Specifically, this is function overloading.
You can also overload operators, like the + sign.
Doing that is called operator overloading and is part of this same type of polymorphism.

This family and type of polymorphism is referred to as ‘Compile Time Polymorphism’.
It is called that because when the compiler translates the data from C# to the machine language the polymorphism is encoded (machine calls do not have names like we see in the code, just addresses).

The first example of ‘Run Time Polymorphism’ was more complex from the computers point of view, so it was figured out after it had been compiled (when the program was actually running).

~~
Again, our human system is way more complex.
All output to another’s system is also reflected back as input into our own system.
Everything we do has feedback, emotions affecting thoughts, and thoughts affecting emotions. Heck even emotions affecting emotions, and of course thoughts affecting thoughts.
However, even for our human systems to accomplish this polymorphism comes into play.

~~

Encapsulations allowed us to organize and group functions and data together.
Inheritance allowed us to build on the shoulders of those code who came before us.
Abstraction allowed us to envision something more than we could fully imagine.
&
Polymorphism let us realize that we can have a call that is more than just a specific function.

Sure, it is true that there is a lot more to cover, but
These four fundamental principles allow for the recognition of development power.


This concludes the Fundamental Article Series.
Stay Tuned for more…

1 Comment

Leave a comment