11th April, 2020 Output

What is polymorphism

 Polymorphism is the condition of occurring in several different forms.

 In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. 

Why you might want to use it to replace conditional logic in your application.

In OOP you will often encounter the abundance of using IF/Else which on the negative side may provide a deep layer of nesting which is increasing the complexity of your solution. Imagine having more than 2 variables that must be tested simultaneously. And also, if a new object property or type appears, you will need to search for and add code in all similar conditionals.

Polymorphism will enable you to change the behaviour of the method by providing a method with the same name in a child's class. 

A detailed video tutorial on how to replace your conditional statement with polymorphism can be found here 

Benefits of using polymorphism over conditional statement in your application

  • Removes duplicate code. You get rid of many almost identical conditionals.

  • If you need to add a new execution variant, all you need to do is add a new subclass without touching the existing code (Open/Closed Principle).

  • This technique adheres to the Tell-Don’t-Ask principle: instead of asking an object about its state and then performing actions based on this, it’s much easier to simply tell the object what it needs to do and let it decide for itself how to do that.

Reference

Polymorphism

Replace Conditional with Polymorphism

Previous
Previous

13th April, 2020 Output

Next
Next

7th April, 2020 Output