Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance Members' values

 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, in future, use tags to preserve the formatting of your code.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls give final conclusion and reply...

thanx in advance...
 
Shweta R Dhaneshwar
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference varibles,where ever inheritance is involved,its the class type of the reference variable which will be given preference.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic