• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism in Overloaded and Overridden Methods

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't like class-casting. You have to be very very careful to avoid errors.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic