0
Can someone please explain to me detaily what polymorphism is all about?
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
0
refers to the idea of"having many forms" it occurs when there's hierarchy of classes related to each other through inheritance.