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 Use of THIS and SUPER : Clarification Needed 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 "Use of THIS and SUPER : Clarification Needed" Watch "Use of THIS and SUPER : Clarification Needed" New topic
Author

Use of THIS and SUPER : Clarification Needed

Vinny Srin
Greenhorn

Joined: Feb 25, 2002
Posts: 6
Folks
If class B extends A and class C extends B and Void Method1() is available in all three classes:
1. Executing super.Method1 from class C will only invoke the method in B and not A ?
2. We can invoke a variable V in Class A from Class C by having a reference variable X of Type Class A, there by accessing V as X.V ?
Basically what is the rule regarding methods and variables if we were to access them from a level that is few levels lower in the hierarchy.
True?
[ February 27, 2002: Message edited by: Vinny Srin ]
[ February 27, 2002: Message edited by: Vinny Srin ]
Lionel Siau
Greenhorn

Joined: Feb 27, 2002
Posts: 12
My 2c pre SCJP worth
1. Yes.
2. Yes. Didn't get what u're saying next inthe 2 statements.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
First, we don't invoke a variable, we invoke a method and access a variable.
1. That's right, the invocation to super.Method1 in Method1 in class C will invoke Method1 in class B.
2. That's correct too.
Your summary is correct:
- variables are accessed according to the type of the reference and
- methods are invoked according to the class of the referenced object and not the class of the reference variable.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Vinny Srin
Greenhorn

Joined: Feb 25, 2002
Posts: 6
Thank you.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Vinny Srin:
[QB]
If class B extends A and class C extends B and Void Method1() is available in all three classes...


1. Executing super.Method1 from class C will only invoke the method in B and not A ?

True. From C, there is no way to directly call a method in A using super. The keyword super only works for the class from which you immediately extend.

2. We can invoke a variable V in Class A from Class C by having a reference variable X of Type Class A, there by accessing V as X.V ?

Assuming that the variable V is accessible, you can access it this way. It will simply follow the normal accessibility rules. You can look at JLS, Section 6.6 - Access Control for more information. (I suddenly feel like Val. )

Variable is invoked by Type of the reference
Method is invoked by Class of the current object.

The second line is true for instance methods but not for static methods. In that case, the compile-time type is used to determine which method should be called. The first line, however, is true. Take a look at the following code to see how this behaves:

I hope this helps,
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I suddenly feel like Val.
Gooooooood boy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of THIS and SUPER : Clarification Needed