• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need "super" keyword understanding

 
Mohtashim Shaikh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic