• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused question from JXAM

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody can help me why the following result is 2, 3?
I just consider that is 3, 3. Could you please tell me the procedure. Thank you very much!
Han
==========================================
class Test {
public static void main(String args[]) {
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base {
int x = 2;
int method() {
return x;
}
}
class Subclass extends Base {
int x = 3;
int method() {
return x;
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, the result should be 3, 3.
Michgen
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is very easy question. Michgen is wrong. If you compile and run, you will get the result.
sog
 
hanmeng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Thank you for replay. I mean why the result is 2, 3. What's the precedure.
han
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, all
this question is a typical question, i have met before.
i think it would like this,
when get variable, it refer to the class, so b.x is Base.x; and method is danymic('cause polymorphism), so it is refer to real method pointer, and b.method() is SubBase.method(), reference to it's real method().
wish can help you
michael
 
hanmeng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, michael
han meng
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
micheal's explain is not clearly.
sog
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Micheal,
you said the : Base b = new Subclass() is belong to Base?
NO, NO, NO. It's belong to Sublcass().
santoo lutina
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
santoo lutina is correct. but why the result is 2?
sog
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think extends Base is the reason to transfer the pointer to the Base . so , the result is 2.
sog
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some one can explain?
Michgen
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just check my note, I find that:
b.x -----> Subclass --------->extends Base ------->Base
------>Base.x=2.
please tell me I am wrong
santoolutina
 
hanmeng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you santoo,
I see.
Han meng

Originally posted by santoo lutina:
I just check my note, I find that:
b.x -----> Subclass --------->extends Base ------->Base
------>Base.x=2.
please tell me I am wrong
santoolutina


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
santoo, why : Subclass ------> extends ------>Base.x?
I am really confused by you. Ha, ha, funny.
michgen
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is correct. These kind of questions have been discussed a lot in passed. So advice for all is to redirect the discussion to something in the past. I dont remember the exact link but I have seen a lot of discussions on this past. Here two points are to be noted. One is the variable and the other is the method. The methods are always accessed by the "object type" and the varaibles are accessed by the "reference type". Now how to determine what is a object and what is a reference. Take this example :-
Base b = new Subclass()
This means that b is a reference of type Base class and is pointing to the object of type Subclass. So
b.variable will access the Base class variable which is 2 here beacuse the reference type is Base
b.method will access the Subclass method which will give 3 here, because the object type here is Subclass.
Thanks,
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, for these kind of questions apply the very simple rule below:
In inheritance, the variables are always shadowed, but for the methods the method of the actual object is invoked using the dyanamic lookup.
So, whenever u have Base b, b.x will always refer to the variable in class b - no lookup is done.
Bur for the methods, the actuall method of the object is invoked.
-sampaths77
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,
I'm visiting here for the first time.This site looks very interesting.
I think answer for this question 2,3 is correct because for a variable, type is considered and for the methods the object of the method is considered.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is from "A Programmer's Guide to Java Certification"
"When a method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of the reference, that determins which method implementation will be executed. When a variable of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determins which variable will actually be accessed."
page 181 2nd paragraph under Variable Shadowing.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic