• 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 wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Dynamic Method Lookup?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why this code can't compile, isn't AClass a subclass of Object? so the method of an instance aClass should be looked for, but why the compiler complains that "aMethod isn't defined in Object."


[This message has been edited by lucy hu (edited January 18, 2001).]
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a compile time check and runtime check runtime it goes for dynamic method dispatch
but compile time it checks whether the type has got a method specified.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lucy!
Think this way:-
You are creating the object reference of 'Object' type and not 'aClass' type.Now the new method defined is in the subclass of Object class which is defined by you,isn't it?Now how can the reference of the superclass know what differences you have made in the suclass???
I hope i have helped you abit in clearing the unwanted doubts!
GOLU
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi lucy,
A reference is like a remote control.
The buttons are your method calls on your T.V set(Your Object) which gladly responds to the button pressed!
Using this analogy,Your Object handle(remote) has only 11 buttons(corresponding to the 11 methods of Object).Using this handle,
you can therefore call only the 11 methods on every other object.
(AClass).
Here you're calling a method (or rather trying to press a button that does'nt exist in your remote.)
Object aClass = new AClass();
aClass.aMethod();
Hence the compile time error!
If your base class "is a" super class ,you can access full functionality of the super class with base reference (Polymorphism).
How ever you cannot access the same for methods that are not defined in the base class with the base handle.
Hope I have'nt confused you!
 
Pravin Panicker
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correction : This is what i meant :
If your sub class has a purely "is a" relationship with base class ,you can access full functionality of the sub class with base reference (Polymorphism).
How ever you cannot access the same for methods that are not defined in the base class with the base handle.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as told by other friends super reference variable is of type O Object which dont have any method aMethod()
so you have to first take a reference of AClass type
means you have to typecast the reference again to AClass
then you can call the method
as shown bt the lines below commented
this way u r not creating the new object just type casting the existing one

public class AClass {
public void aMethod() {
System.out.println("subclass's method");
}
public static void main(String args[]) {
Object aClass = new AClass();
//AClass ac = (AClass) aClass;
//ac.aMethod();
}
}
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi luci,
compiler chaecks whether the method is present in the calling method. This is logical bcoz the compiler will be sure that even if the object referred in the variable doesnt have the method still there wont be any problem bocz it can call the method of the super class.
Cherry
 
lucy hu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help!
And Pravin's analogy is really impressive!!! I think i got what you mean, and change some of them in this way:
1) TV set: an object.
2) Buttons on TV: Object's 11 methods.
3) Buttons on Remote Control: Subclass's methods, there maybe some more buttons, like those buttons control VCR.
Now, I don't have a VCR connected with my TV, but if I press those buttons control VCR(call those methods that are defined in subclass, but not in Object), ofcourse they doesn't work.
After typed cast, the code should be like this, and it works fine now:

Lucy
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a compile time problem.
Remember compiler just looks at your code superfacially. When you declare aClass, you declare it as an Object type, really a reference type. At compile time, there is no object new AClass() created yet. And when you invoke aClass.aMethod(), the compiler has to go to Object class to look for this method. And it certainly will generate an erro message as no such method exists.
Hope it clears up.

Originally posted by lucy hu:
[B]I don't know why this code can't compile, isn't AClass a subclass of Object? so the method of an instance aClass should be looked for, but why the compiler complains that "aMethod isn't defined in Object."

[This message has been edited by lucy hu (edited January 18, 2001).][/B]


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic