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

The super keyword

Tess Jacobs
Ranch Hand

Joined: Feb 07, 2012
Posts: 71
    
    3
A class can use Outer.this or this to access its inner class. For example



A class can use Outer.super to access the inner class of its superclass. Why can't it use super to access the inner class of its superclass. For example

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Tess Jacobs wrote:A class can use Outer.this or this to access its inner class.

Or indeed, nothing at all. For example:
A class can use Outer.super to access the inner class of its superclass. Why can't it use super to access the inner class of its superclass.

As for this one, I'm not quite sure; but it seems awfully tortuous. Inner classes (and BTW, this is NOT an inner class, and neither is super) are relatively rare, so it's possible that the designers didn't think the construct was worth worrying about.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You would have to go through the Java Language specification and see what it says. Look carefully at the grammar. Beware: it ain’t easy to read.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40



First, I think it should be noted that there is no such a thing as a super object. The this object is the current instance whose method is being called. And the super keyword is used to help clarify which part of that this object. It is *not* a separate object.

So, what does the super keyword do? If you use it for a method, it means to calls the original method that has been overridden. If you use it in a constructor, it means to call the superclass constructor. If you are using it to get to an instance variable, it is telling the compiler to use the variable (which may be hidden) in the superclass version. For this last case, you can also use the class name prior to the super keyword -- which interestingly allowed you to do something that I thought would not work (learned something new).

Anyway, again, there is no super object. So this...



can be replaced with this...



and as you also noticed, this seems to also work...



which probably is needed, if there is another Inner class definition that is hiding the one in the super class definition.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tess Jacobs
Ranch Hand

Joined: Feb 07, 2012
Posts: 71
    
    3
The question is - since Outer.this can be replaced with this (within Outer class), it should be possible to replace Outer.super with super (within Outer class). This is true when a class needs to use the super keyword to access an instance method in its super class, but for some reason, it doesn't work when a class needs to use the super keyword to access an inner class in its super class (without having to create an instance of its super class).

I am assuming that what @Winston said is true. The designers didn't think the construct was worth worrying about when a class needs to access an inner class in its super class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tess Jacobs wrote:The question is - since Outer.this can be replaced with this (within Outer class), it should be possible to replace Outer.super with super (within Outer class).


That seems like reasonable logic on the surface, but it's an assumption based on the premise that super and this are completely structurally equivalent, when in fact, they are not.

This is true when a class needs to use the super keyword to access an instance method in its super class, but for some reason, it doesn't work when a class needs to use the super keyword to access an inner class in its super class (without having to create an instance of its super class).


I haven't looked in much detail at your classes or what you're asking, however, one thing that may (or may not) be relevant is that we can't refer to a nested class's c'tor using EnclosingClassName.NestedClassName() when that nested class is non-static.



Tess Jacobs
Ranch Hand

Joined: Feb 07, 2012
Posts: 71
    
    3
Tess Jacobs wrote:The question is - since Outer.this can be replaced with this (within Outer class), it should be possible to replace Outer.super with super (within Outer class).


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Tess Jacobs wrote:
Tess Jacobs wrote:The question is - since Outer.this can be replaced with this (within Outer class), it should be possible to replace Outer.super with super (within Outer class).

{Lots of code}

And your point is?

I think we've tried to explain why it may be so, but the fact is that it's not the only anomaly you're going to find in the language (if indeed it is one).
As I said, the need for true inner classes is relative rare, so it's possible that this was simply an oversight; and even if it is one, the chances of it ever causing a problem are virtually nil.

Winston
Tess Jacobs
Ranch Hand

Joined: Feb 07, 2012
Posts: 71
    
    3
Winston Gutkowski wrote:
And your point is?

Thanks @Winston. I accepted your answer a while ago. Based on the answer provided by @Jeff, I felt that my question wasn't clear enough, so I wanted to make it a little clearer.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Tess Jacobs wrote:Thanks @Winston. I accepted your answer a while ago. Based on the answer provided by @Jeff, I felt that my question wasn't clear enough, so I wanted to make it a little clearer.

Oh, OK. Fair enough. I find that sometimes beginners obsess about things that aren't really worth their time, so I try to steer them off it. Apologies if I sounded snippy.

I should probably also add: well spotted.

Just a BTW: You may notice that I broke up that very long line in your code because it screws up the window formatting here, making your thread hard to read. I suggest you re-read the UseCodeTags page in full, because it explains about this.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The super keyword