File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The super keyword

 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic