you are creating the dog object and giving it to a animal reference.
Such situation might occur when you create List Object.
List is a interface , so you create a Object for arrayList or any of the classes that implements List and referenc it to List .
This answer iam expecting. Actually my doubt starts here.we can directly write
ArrayList al = new ArrayList();
List l = new ArrayList();
What is the use in instantiating ArrayList and giving reference to List.?
Typically, It is better to program with super class/interface reference, this avoid the a class specific implementation method call in your code. for example,
pretend you love Dog now,
after some time you decided to go for a cat since you dont like the Dog
if you could have been used like this
in(1), you may tempted to use Dog class specific method, later when you change Dog to Cat
you are required to change the line (1) also , so totally two lines are modified, so maintenance may be nightmare there!
this is a small example with two lines, think about thousands of line!
Joined: Oct 14, 2010
Thanks Seetharaman ,
Is this the only reason or any other logical reason. When i see that kind of statements iam confused a little bit.
and this is called as Run-time polymorophism,right?
can anyone tell me a real time example of this run-time polymorphism.