Can someone please explain to me detaily what polymorphism is all about? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone please explain to me detaily what polymorphism is all about?

18th Mar 2016, 2:50 PM
MOUZONG DJAM Andreas
MOUZONG DJAM Andreas - avatar
2 Answers
+ 3
well the word poly means many and morphe is form so many forms so if you say that a method is polymorph than it has different shape in multiple class so for example we have a class A that has a method called identify() that prints "i am A" and a class B that extends A and adds a new method id that's prints "hi" and overrids the method identify() so it prints "i am B" so the method identify has tow forms depending on the object's class it's called from that's for a polymorphe method .now a polymorphe variable let's take the previous A and B classes if i said A a = new A();a.identify(); i wont get an error and it prints i am A as usual B b = new B();b.identify(); i wont get an error and it prints i am B as usual A a = new B();a.identify();wont throw an error but prints i am B so what is happening well the compiler goes to the declared type "A" and checks if it has the method identify() if he finds he wont throw an error else he will throw an error
1st Jun 2016, 5:45 PM
Imad Eddine Ibrahim Bekkouche
Imad Eddine Ibrahim Bekkouche - avatar
0
refers to the idea of"having many forms" it occurs when there's hierarchy of classes related to each other through inheritance.
12th Aug 2016, 8:06 AM
Joyx Kish
Joyx Kish - avatar