aspose file tools*
The moose likes Java in General and the fly likes Polymorphism in Overloaded and Overridden Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Polymorphism in Overloaded and Overridden Methods" Watch "Polymorphism in Overloaded and Overridden Methods" New topic
Author

Polymorphism in Overloaded and Overridden Methods

Bhaskar Jakkaraddi
Greenhorn

Joined: Oct 28, 2010
Posts: 2


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

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Mar 12, 2010
Posts: 79
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

Joined: Oct 13, 2005
Posts: 39826
    
  28
And welcome to the Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
I don't like class-casting. You have to be very very careful to avoid errors.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism in Overloaded and Overridden Methods