-2

i am confused with run-time polymorphism and compile-time polymorphism...

considering the following code snippet :

if(  someClassObject.returnRandomNumber() == 1 )
  new objectOfClass1();
else
  new objectOfClass2();

So, will it be called dynamic polymorphism.???

Thanks in advance.

laxman
  • 1,781
  • 4
  • 14
  • 32
  • read about overloading and overrding concepts of oops. – rathna Mar 02 '17 at 12:39
  • You should try to come up with a better code snippet to explain your question - the one you posted does not do anything (it creates an instance and then "throws it away" right after) – UnholySheep Mar 02 '17 at 12:42
  • soory. fildor just updated the code,i am a beginner :( – laxman Mar 02 '17 at 12:49
  • You're welcome :) - As is, your example has very little to do with any kind of polymorphism. The term "Polymorphism" is also not specific to Java and has to be somewhat interpreted how the language is providing different kinds of it. What you refer to as "complie-time polymorphism" would probably be method **overloading**. While run-time or dynamic will be method **overriding**. – Fildor Mar 02 '17 at 12:55
  • See http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading – Fildor Mar 02 '17 at 12:56
  • Thank you Fildor, that really helped me !! :) – laxman Mar 02 '17 at 14:29

1 Answers1

-1

Polymorphism

Run time polymorphism and dynamic binding are same. We have too many words for it i.e. Dynamic binding or Run-Time binding or Late binding or Method overriding.(in two different classes)

if( someClassObject.returnRandomNumber() == 1 )
   new object1;
else
   new object2;

In the above code if you have returnRandomNumber() method in two different methods. So it's up to you which class method you want to call with that appropriate class object. For more details

Community
  • 1
  • 1