File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of THIS and SUPER : Clarification Needed

 
Vinny Srin
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinny Srin
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suddenly feel like Val.
Gooooooood boy
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic