Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which version of method is actually called ? the reference one or actual object one ?

 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B book,there is an example



And then it says,

Animal c = new Horse();
c.buck(); // Can't invoke buck();
// Animal class doesn't have that method

Why will it not check for Horse class for buck method ?

Can somebody help me out in making clear picture when methods on reference variable are called and when methods on actual object is called ?
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in case of overriding method on object type is called and in case of overloading method on reference type is called
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Animal c = new Horse();
c.buck(); // Can't invoke buck();
// Animal class doesn't have that method

Why will it not check for Horse class for buck method ?


I remember sometime ago seen similar post and the reply impressed me was

" Animal reference cannot used to call buck() method " because simply there is no button( buck) in the remote (Animal) to use
 
Rajdeeep Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Agr wrote:

Animal c = new Horse();
c.buck(); // Can't invoke buck();
// Animal class doesn't have that method

Why will it not check for Horse class for buck method ?

Can somebody help me out in making clear picture when methods on reference variable are called and when methods on actual object is called ?


Compiler will try to look for a buck() method in Animal and will not find it. So, this will not even compile
 
Mumtaz Khan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Reference variable's type not the object's type determines which method can be called and polymorphic method invocation apply only to overridden method. Thats why this code
runs eat() method of Horse.
As eat() method is an overridden method and Polymorphism applies to overriding so object's type( here Horse) method runs.
Here the reference type is Animal which doesn't have buck() method so it will not compile. buck() is not an overridden method but an instance method of Horse. Calling of method is determined at compile time based on reference type






 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Animal reference cannot used to call buck() method " because simply there is no button( buck) in the remote (Animal) to use



This is a great explanation, it looks like a Sierra & Bates Head First quote!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Agr, welcome to JavaRanch.

Please use code tags when you post source code.
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright. I didn't know before , will use code tags from now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic