aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which version of method is actually called ? the reference one or actual object one ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which version of method is actually called ? the reference one or actual object one ?" Watch "which version of method is actually called ? the reference one or actual object one ?" New topic
Author

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

A Agr
Ranch Hand

Joined: Feb 10, 2009
Posts: 32
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

Joined: Jan 31, 2009
Posts: 70
in case of overriding method on object type is called and in case of overloading method on reference type is called


We will keep things moving!!
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


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

Joined: Feb 10, 2009
Posts: 3
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

Joined: Feb 21, 2008
Posts: 53
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







SCJP 5.0 -- 97%
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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!


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

A Agr, welcome to JavaRanch.

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
A Agr
Ranch Hand

Joined: Feb 10, 2009
Posts: 32
Alright. I didn't know before , will use code tags from now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which version of method is actually called ? the reference one or actual object one ?