• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

super and sub varible

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
////////////
public class Test extends Sup {
private int i = 1;
public static void main(String args[]) {
Sup s = new Test();
System.out.println(s.i);//print out 0! confirmed by coding
}
}
class Sup {
protected int i = 0;
}
//////////////////
Why zero is print out? I think it would be one. As I know, to call a method (which is defined in sub and sup), the most specific method will be called. Why not the member varibles?
Thank you very much.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Tony Xu:
Why zero is print out? I think it would be one. As I know, to call a method (which is defined in sub and sup), the most specific method will be called. Why not the member varibles?
Thank you very much.


Hi Tony,
The methods called on an object depend on the actual type of the object itself, not the declared type.
The variables called on on object depend on the declared type of the object regardless of the object's actual type.
The code is returning 0 because that's the value of i in Sup, the declared type of s.
--liz
------------------
Elizabeth Lester
SCJP Dreamin'
 
Tony Xu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thanks.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
Well,Elizabeth, has explained good but i am adding more to his explanation.
1: Methods are called by the "actual type" of object.
Explanation:
"Actual type" of the object is the constructor by which abject is created(using new).
i.e if Sup s = new Test();
then new Test() will create the object whose type is Test.i.e actual type of object is Test.
s.method() will call the method of Test Class.
2:Variable are called by type of reference.
Example:
if Sup s = new Test();
Then type of refernce is "Sup".And "s.variable" will refer to the variable present in Sup.
Regards,
Hassan
Note: If i am wroong then pls. do correct me.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding point 1:
You are right. But it is useful to remember that private and static methos can't be overriden. Thus they don't have polymorphic behaviour. That means they are bound to the declaraction in the type of the variable.
 
Elizabeth Lester
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hassan and Jose,
Thanks for your additional explanations! I knew my answer above was brief and incomplete...I was studying for my exam this morning but I wanted to give some sort of response.
--liz
------------------
Elizabeth Lester
Sun Certified Programmer for the Java� 2 Platform
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic