• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Need "super" keyword understanding

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Marshal
Posts: 22457
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic