• Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorphism  RSS feed

 
Abdul Aatif
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me, what is the use of polymorphism??
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Latif Khan wrote:can anyone tell me, what is the use of polymorphism??

It allows subtypes to exhibit different behaviour for the same API.

The classic example is a Shape class (or interface) with a draw() method. Subtypes of Shape (eg, Circle, Square, etc) implement draw() in a way that's specific to their own particular shape. Then you can have code like:and each Shape will be drawn the correct way.

HIH. If not, I suggest you look at the Java tutorials.

Winston
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have any idea of over riding methods ??? it works on polymorphism ...

and there are other such things too ... like polymorphic references and even polymorphic argument types
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:have any idea of over riding methods ??? it works on polymorphism ...
and there are other such things too ... like polymorphic references and even polymorphic argument types

Well in my example, the polymorphic reference would be 'shape' and the polymorphic argument type would be Shape.

But it sounds to me like you really need to read the Java tutorials.

Winston
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the suggestion dear "Gutkowski" but what is wrong with method overriding in the case of polymorphism ... can't overridden methods take polymorphic references as arguments ... and yes i read the example in your link. even they have used overridden methods
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:what is wrong with method overriding in the case of polymorphism


Nothing is wrong with it. And he didn't say that anything is.

... can't overridden methods take polymorphic references as arguments ...


Method signatures are not runtime polymorphic.

At compile time, we determine which exact signature to call. At runtime, that exact signature is used. The part that is determined at runtime is that we decide which class's implementation of that exact signature to invoke.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know overridden methods are not runtime polymorphic ...

By this ...
... can't overridden methods take polymorphic references as arguments ...


I meant



and this
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:I know overridden methods are not runtime polymorphic ...


That is false. Overridden methods are the only thing in Java that is runtime polymorphic.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Method signatures are not runtime polymorphic.


you said this ... i meant this only ...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:

By this ...
... can't overridden methods take polymorphic references as arguments ...


I meant



and this


Which exact signature to invoke is determined by reference types at compile time. It is only which class's implementation of an exact signature to call that is determined by the actual object's class at runtime.

In your first example, each subclass has its own implementation of the exact signature whoAmI(). However, in the second example, we determine at compile time that you are calling the exact signature whoAmI(Animal). That is not overridden, because no subclass has a method with that signature. So, at runtime, when we look for whoAmI(Animal), the only implementation we find is in Animal, so we call that one.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well in my first example if you can see ...

all references are of type Animal ...

and so all the method calls are based on the type of object that these references refer too which are different ... so methods from different classes will be called

in my second example the method is same but i am passing child classes as arguments ... which again is an example of polymorphic reference as i can pass a subtype where a supertype is required
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:well in my first example if you can see ...

all references are of type Animal ...

and so all the method calls are based on the type of object that these references refer too which are different ... so methods from different classes will be called


Correct. That is Java's runtime polymorphism. This is what I have been trying to explain. My apologies if I wasn't making it clear.

in my second example the method is same but i am passing child classes as arguments ...


Which means they are not the same methods as far as Java's runtime polymorhpism is concerned.

which again is an example of polymorphic reference as i can pass a subtype where a supertype is required


This is method overloading (which is different from overriding). It is also generally considered a form of polymorhpism, however, some people don't consider to be. The key difference, however, is that this form of polymorphism happens entirely at compile time, while method overriding happens at runtime.

So, once again...

At compile time: We determine exactly which signature will be invoked.

We figure out which methods could match. Because ref1 is of type Animal, we look only at methods that are declared in Animal or one of its supertypes and that take an arg that is a reference of type Animal, or the "closest supertype" of Animal (so if there wasn't one that takes Animal, but there was one that takes Object, we'd get the Object one). The one that we find is Animal.whoAmI(Animal). This means that right now, at compile time, we have decided that we will call the method with exactly the signature whoAmI(Animal). Not whoAmI(Dog) or anything else. This decision has been made and will not change at runtime.

At runtime: We determine which class's implementation of exactly that signature will be invoked. The object we have is of type Dog, so we look at Dog, but it does not have a matching method. So we look at its parent, Animal, and we see that, unlike Dog, it does have a method with the exact signature whoAmI(Animal), so that is what we invoke.

Do you understand now, or is something still not clear?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!