Why Polymorphism ?! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why Polymorphism ?!

why we need 'Polymorphism' , what is our need to it ?? for example : we have 2 classes called C , D inherit from class A , each of them contains d() method that prints a diferent text . and we want to print its content after objects definition for the subclasses(C, D) this the way : C c= new C(); D d= new D(); c.d(); d.d(); this the usual way we learnt but with Polymorphism : A a1= new C(); A a2= new D(); a1.d(); a2.d(); isnt the first way of defintion is enough ? this is more diferent and complex . that makes my mind frozen . In fact this section is so hard to understand i have many question about Poly.. but the only question makes me confuse me is why Polymorphism ?

23rd Feb 2019, 12:47 AM
Nakamatori
Nakamatori - avatar
1 Answer
+ 5
When done right, your main processing code only deals with class A. It never needs know C or D or some future class exists. Therefore, adding E to the system based on A doesn't need as many changes. You create E and it is done running as if it was there from day one.
23rd Feb 2019, 2:47 AM
John Wells
John Wells - avatar