aspose file tools*
The moose likes Java in General and the fly likes Need Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need "super" keyword understanding" Watch "Need "super" keyword understanding" New topic
Author

Need "super" keyword understanding

Mohtashim Shaikh
Ranch Hand

Joined: Jul 18, 2010
Posts: 43


I can use the variable i in class B using super.. as super.i
My question is how would i call variable i of class A in class C and class D.
Can someone help me with the code and explaination? Also super().super() works for constructors of the grandparent class; then why not for other methods and variables?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

super refers immediate super class object. you cant use idioms either super().super() or super.super.instancevariable , it is a syntax error.

* when you are instantiating a subclass then super class constructor runs and then its super class constructor runs and so on... that is because compiler insert *super()* in every class constructor as a first statement.
Shamayla Bukhari
Greenhorn

Joined: Jun 24, 2011
Posts: 4
I am not sure what you are trying to accomplish here.
The class A has a variable i and B is inherited from A. So, why are you re declaring i again in B and hiding the i in A.It sort of kills the whole purpose of inheritance doesn't it? B already has i just use it.
To answer your questions
My question is how would i call variable i of class A in class C and class D.

If in B you re declare i you cant. you just have access to the one in the immediate parent using super keyword.
If you don't re declare it you can access and use it in any method by just using i=25; without super keyword.
And as far as I know
Also super().super() works for constructors of the grandparent class; then why not for other methods and variables?

This should and does not work.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

For methods you can only go up one level. In class D you can call the C version of hello() using super.hello(), but you can't call the B or A version of hello().

For fields, the reference type is used. In class D, i means the D version, and super.i means the C version. But unless the access modifier (private etc) blocks access, you can still access the B and A versions:
That's because for fields, the reference type is always used. by casting the current object (this) to B or A the compiler uses that class (B / A) for the reference type, allowing you to access its fields. Note that instead of super.i you could have written ((C)this).i.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
The example also show how confusing it can be to have fields of the same name in superclass and subclass. Are you using the A i or the B i at a particular moment?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Need "super" keyword understanding