wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance Members' values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instance Members Watch "Instance Members New topic
Author

Instance Members' values

Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
How does the following code work :

Output:
2 5 4
2 10 8

How the first line second and third colum is of parent instead of child?
[ May 31, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please, in future, use tags to preserve the formatting of your code.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Because t is a reference variable of class type Test and not a object
type T


Only instance methods will be invoked based on object type. Instance variables will be invoked based on class type.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by vishnu prakash:
Because t is a reference variable of class type Test and not a object
type T


Only instance methods will be invoked based on object type. Instance variables will be invoked based on class type.



i didn't get the point here....

why in System.out.println(t.i+" "+t.j+" "+t.k);

t.i is producing 2 and not 1

as t is a reference variable fo Test it will call the instance variable from Test class ( this part is clear )
but when calling t.i (from Test) which intun is calling fun() ...according to result the method from class T is calling

i know you will say as object is of type T it will call method from class T
but my confusion is that when i=fun(); is invoked in super class Test how it how it will call fun() method from subclass...as it may not know that it exist...

so is the following is the final conclusion. ?
so is this is a case that always and always subclass method will be called if object is of subclass....and call of method can be from any where in the package...

and variable of refernce type will be called ...even called from any where..


do reply


Thanks and Regards, Amit Taneja
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
pls give final conclusion and reply...

thanx in advance...
Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
For reference varibles,where ever inheritance is involved,its the class type of the reference variable which will be given preference.


Shweta Dhaneshwar.<br />SCJP 1.4 90%
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
To understand how this code works, you need to understand polymorphism. Basically, polymorphism allows the program to decide which method to call at run-time. Sometimes this is called "late binding". Notice that polymorphism only works with methods; it doesn't work with data fields. This means that the line

can dynamically decide which version of fun() to called based on the objects actual type. However, when you have a line like

the compiler decides which variable is assigned the value. This means that the value of the variable i is dynamic (i.e. determined at runtime), but the value of the two k variables is static (i.e. determined at compile time).

Similarly, the compiler decides which k variable to access at compile time. This is why you only see the value from the parent class and not from the sub class. Since t is a reference to Test, the compiler only "sees" the parent class and its fields.

I'm sure this explanation is clear as mud. Please let me know if I need to clarify anything.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance Members' values