File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain(about accessing members)! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain(about accessing members)!" Watch "Please explain(about accessing members)!" New topic
Author

Please explain(about accessing members)!

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
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 ]

Software_guy
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
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)


Rich
SCJP 1.4
Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53
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 ]

When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
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

Joined: Nov 07, 2003
Posts: 117
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

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

Joined: Mar 19, 2004
Posts: 87
Another way to look at the original question and the diagram: fields are not overriden. Appropiate?
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
Oh!I got it.Thanks.
 
 
subject: Please explain(about accessing members)!