jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference types and Overloaded methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference types and Overloaded methods" Watch "Reference types and Overloaded methods" New topic
Author

Reference types and Overloaded methods

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 132
Hi Folks,

Please do have a look at the following three classes:

Animal



Horse



OverrideOverload


I was expecting an error when I run this program, but instead it successfully invoked the eat() method from the animal class!!! The reason for me expecting an error was because I was attempting to invoked the eat() method from the Horse class, but with a String argument instead of an int that the Horse class's eat() method was expecting. I understand that it's the reference type that determines which overloaded method will be used, so I am surprised that instead the Animal class's eat() has been invoked when my code says: Why is this so? Hope somone can shed some light on this. Thanks.

regards
John
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
John Paterson wrote:I was expecting an error when I run this program, but instead it successfully invoked the eat() method from the animal class!!! The reason for me expecting an error was because I was attempting to invoked the eat() method from the Horse class, but with a String argument instead of an int that the Horse class's eat() method was expecting. I understand that it's the reference type that determines which overloaded method will be used, so I am surprised that instead the Animal class's eat() has been invoked when my code says: Why is this so? Hope somone can shed some light on this.


Based on your code, Horse IS-A Animal. So, Horse will inherits (public, protected and default) variables and methods of the Animal(its Super Class).
Therefore, Horse has two methods one with String argument(i.e. inherited) and other with int argument(its own).
Thus you will get your result...
John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 132
Hi Gaurangkumar Khalasi


Thanks for the clarification.

regards
John
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
I might be wrong, but seems there is no overriding in Paterson's example. Right guys?

Once eat(String) and eat(int) works as overloaded methods, because we are just changing it's signature, overring does not apply here. There is no "same" (name + arguments) method here.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
John Paterson wrote:Thanks for the clarification.

You are welcome.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Guilherme Bazilio wrote:I might be wrong, but seems there is no overriding in Paterson's example. Right guys?

Once eat(String) and eat(int) works as overloaded methods, because we are just changing it's signature, overring does not apply here. There is no "same" (name + arguments) method here.


Yes, you are right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference types and Overloaded methods