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

Please explain(about accessing members)!

 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that the output of the following code is 2,sub, but it's given as 5,sub.Can anybody explain me in details pl.I uderstood the reson for getting sub but bit confused with the value 5.

Modified title to be a little more informative and added code tags...
[ April 10, 2004: Message edited by: Barry Gaunt ]
 
Richard Quist
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sridhar Srinivasan:
I thought that the output of the following code is 2,sub, but it's given as 5,sub.Can anybody explain me in details pl.I uderstood the reson for getting sub but bit confused with the value 5.
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class MyFor
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();

}
}

As you know, your Sub class has overridden the printVal method and the method to call is picked at runtime. BUT...the choice of which index to use was made at compile time - based on the reference type of the variable (in this case Super)
 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be explained as follows

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 determines which methods or implementation will be executed. (Dynamic Method Lookup)
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 determines which variable will actually be accessed.
Whew....
[ Jess adjusted the location of the end [code] tag to allow the lines to break so the screen wouldn't stretch too far ]
[ April 09, 2004: Message edited by: Jessica Sant ]
 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow,
Yogesh thanks a lot about the diagram.I guess i will never be confused here after when i get such a question.Your diagram is a class aprt.
Thanks a lot.
Ramnath
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the detailed explaination.One more question related to this.If the method is overloaded(not overridden), then the method will be of the reference type and not the object type.Am I rite?Please advice.Thanks a lot!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sridhar Srinivasan:
If the method is overloaded(not overridden), then the method will be of the reference type and not the object type.Am I rite?


If I understand your question correctly, you mean what happens if class Sub has a method like: void printVal(int me) { System.out.println(me);} ?
In this case sup.printVal(42) in the main method will not compile at all, because class Super does not have such a method.
Or do you mean that class Super has the printVal(int me) method? In that case there is no problem in calling sup.printMe(42).
[ April 10, 2004: Message edited by: Barry Gaunt ]
 
Lionel Orellana
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to look at the original question and the diagram: fields are not overriden. Appropiate?
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh!I got it.Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic