• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism in Overloaded and Overridden Methods

 
Bhaskar Jakkaraddi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Case 1


Case 2



In above two case method invocation case 1 works fine Case 2 returns compiler error, would someone explain in depth, for me both looks like method invocation by reference.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading is having multiple methods with the same name, but with different signatures (the argument types). This is resolved at compile time. The compiler works out which one to call based on the reference type.

Overriding is when you inherit a method, but redefine it (with exactly the same name and signature) in the subclass. Overridden methods are resolved at run-time (which is where polymorphism comes in), and is determined by the actual type.

So in your case 1, the reference type is Animal. Animal has an eat() method, so the compiler is happy. Then, at run-time, the JVM realises it's actually a Horse and calls the Horse version of eat(). The output should be "Horse eating hay".

In case 2, the reference type is Animal. Animal does not have an eat(String) method, so the compiler is unhappy. The compiler doesn't try to work out what the actual object is, all it knows is that it's an Animal. So it can't allow that call.

Does that help?
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it will be compiler error in case 2.

Horse class is a descendant to Animal. Animal is more 'wide' class, then the Horse.

In terms of 'is-a' Horse is-an Animal, but Animal is-not a Horse.

It's correct to assign Horse instance to Animal variable.

But on compile time you still are working with Animal variable, and Animal doesn't have 'eat(java.lang.String)' method, only 'eat()'.

To evade this error you should make explicit type-conversion:



This type conversion will be correct as on compile-time (calling existed method of Horse class), as on runtime (because ah2 variable contains Horse instance).
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like class-casting. You have to be very very careful to avoid errors.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bhaskar and welcome to the JavaRanch.

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I'll add them for you since you are new so that you can see the difference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic