Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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






 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright. I didn't know before , will use code tags from now.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic