Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Reference types and Overloaded methods

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurangkumar Khalasi


Thanks for the clarification.

regards
John
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Paterson wrote:Thanks for the clarification.


You are welcome.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic