• 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

question about reference and method invoke

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering the following code:
class Point{
void aMethod(int f){
int var = f;
System.out.println("This is the superclass"+var);
}
}
public class Subpoint extends Point{
public static void main(String[] args){
Point s=new Subpoint();
s.aMethod(2.1f);
}
void aMethod(float f){
float var = f;
System.out.println("this is subclass"+var);
}
}
when compiled, error" aMethod(int) can not be applied to (float)
s.aMethod(2.1f);" occurs
I am confused about,since s references a Subpoint object,why does't compiler invoke aMethod(float) defined in Subpoint class without error?
I remember that when invoke an instance method, the method defined in the class whose object is being referenced will be invoked.
Am I wrong?
Thanks for all replies.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi zhaobin,
The compiler is right in complaining, cos Point doesn't have aMethod(float), though Subpoint does. So this is not overriding, you're just overloading aMethod (that is, now you have 2 versions of it in Subpoint).
Try adding aMethod(float) to Point, and the Subpoint's version of that method will be correctly invoked.
Cheers

Originally posted by zhaobin74:
Considering the following code:
class Point{
void aMethod(int f){
int var = f;
System.out.println("This is the superclass"+var);
}
}
public class Subpoint extends Point{
public static void main(String[] args){
Point s=new Subpoint();
s.aMethod(2.1f);
}
void aMethod(float f){
float var = f;
System.out.println("this is subclass"+var);
}
}
when compiled, error" aMethod(int) can not be applied to (float)
s.aMethod(2.1f);" occurs
I am confused about,since s references a Subpoint object,why does't compiler invoke aMethod(float) defined in Subpoint class without error?
I remember that when invoke an instance method, the method defined in the class whose object is being referenced will be invoked.
Am I wrong?
Thanks for all replies.


 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi zhaobin,
Just looking more deeper into it we can analyse that the
superclass has no idea about what its subclass adds to it.
In your case you have added new method in the subclass which
the superclass dosent know.
Just try to access the subclass variable declared in the subclass
throught the superclass reference type. It will scream. Methods
are also simillar. Only overriden methods can be invoked using
the superclass reference pointer. You cannot invoke methods
declared newly in the subclass by a superclass reference variable
even though there is a object of subclass type.
Hope this helps

------------------
Regards,
V. Kishan Kumar
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Only overriden methods can be invoked using
the superclass reference pointer. You cannot invoke methods
declared newly in the subclass by a superclass reference variable
even though there is a object of subclass type.

Kishan:
I have quoted your posting above but I have a doubt.
Please refer to a similar discussion below: http://www.javaranch.com/ubb/Forum24/HTML/004520.html
As you can see print() method in class Chield is an overridden method. (Remember, methods can be overridden to be more public and this is one such case). Still, when you say P1 = new Chield() and try to print P1.print(), you are not invoking the overridden method in this case! The statement you have quoted may not hold true for all cases. If you disagree with me, please let me know. This way, we can get our concepts clear.
I think this is a case of how late binding is done. I could not get enough explanation from RHE. Wish someone could highlight this better.


Hope this helps

[This message has been edited by Viji Bharat (edited September 29, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic