0

In java, does polymorphism only stands for a child object being referred by a parent reference?

for example: if cat & dog extends Animal,

Animal a = new Dog(); //polymorphism happens here
Animal b = new Cat(); //here too.

Is this the only kind of polymorphism in java? (whether the usage is in collections or simply as in the example).

This may be a novice question, but i am hoping, there is more to this scene & experienced minds can share their thoughts. Thanks.

Ashok Raj
  • 444
  • 6
  • 25
  • 1
    I would advise against using the terms "child" and "parent" when it comes to inheritance. Use "derived from" and "base class" instead. A car isn't a *child of* a vehicle, it *derives from* a vehicle. – dcastro May 07 '14 at 09:42
  • Perhaps this is answered by [What is Polymorphism?](http://stackoverflow.com/questions/1031273/what-is-polymorphism) – Steve C May 07 '14 at 09:47
  • @dcastro he he...sure, i just want to say it in simple words ;) – Ashok Raj May 07 '14 at 12:24
  • @SteveC sorry, but that doesn't answer me, i am already sure of what polymorphism is. I just wanted to know does it exits in java in any other form that the above one. – Ashok Raj May 07 '14 at 12:25

1 Answers1

1

you can see by doing just reference makes no sense. Flavor comes when you call method on that object where Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

The JVM calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is referred to as 'virtual method invocation' and demonstrates an aspect of the important polymorphism features in the Java language.

niiraj874u
  • 2,180
  • 1
  • 12
  • 19