Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDK 1.7 vs JDK 1.6 inner classes inheritance difference

I'm on solving some Java puzzles and stumbled on this one:

public class Outer {
    class Inner1 extends Outer {}
    class Inner2 extends Inner1 {}
}

While compiling this code with javac 1.6.0_45 I'm getting, as expected, this error:

Outer.java:8: cannot reference this before supertype constructor has been called
class Inner2 extends Inner1 {}                                                                                                
^

This is because of compiler generates default constructor for Inner2 class with similar code, which explains error above:

Inner2 () {
    this.super();
}

And it's obvious now, because you really can't do this in Java 1.6.0_45, JLS 8.8.7.1 (as I can guess):

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

See (accepted answer in Odd situation for "cannot reference this before supertype constructor has been called")

But if I try to compile it with javac 1.7.0_79 - it is OK!

And here goes the question - What has been changed in Java 1.7, that this code is now correct?

Thanks in advance!

like image 210
ar4ers Avatar asked Mar 07 '16 20:03

ar4ers


People also ask

Do inner classes get inherited?

Inner classesA inner class declared in the same outer class (or in its descendant) can inherit another inner class.

Is there any difference between an inner class and nested class?

In Java programming, nested and inner classes often go hand in hand. A class that is defined within another class is called a nested class. An inner class, on the other hand, is a non-static type, a particular specimen of a nested class.

What is the difference between an inner class?

inner classes are in the same file, whereas subclasses can be in another file, maybe in another package. You cannot get an instance of an inner class without an instance of the class that contains it. inner classes have the methods they want, whereas subclasses have the methods of their parent class.

What are the main disadvantages of using Java inner classes?

Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code.


1 Answers

Looks like there was discussion of the same problem as the bug JDK-6708938: Synthetic super-constructor call should never use 'this' as a qualifier on the Java bug tracker.

Also I think it would be great for you to take a look at other Related issues of previous one, for example JDK-4903103: Can't compile subclasses of inner classes .

Notice the Fixed Versions of both bugs.

And as the result see Maintenance Review of JSR 901 (Java Language Specification) for Java SE 7.

From The Java Language Specification Third Edition

Otherwise, S is an inner member class (§8.5). It is a compile-time error if S is not a member of a lexically enclosing class, or of a superclass or superinterface thereof. Let O be the innermost lexically enclosing class of which S is a member, and let n be an integer such that O is the n th lexically enclosing class of C. The immediately enclosing instance of i with respect to S is the n th lexically enclosing instance of this.

And from Maintenance Review of JSR 901 (Java Language Specification) for Java SE 7 (full version, page 242, blue text) or the same in The Java Language Specification, Java SE 7 Edition (just before section 8.8.8)

Otherwise, S is an inner member class (§8.5).

Let O be the innermost lexically enclosing class of S, and let n be an integer such that O is the n'th lexically enclosing class of C.

The immediately enclosing instance of i with respect to S is the n'th lexically enclosing instance of this.

So you can see that the part with compile-time error has gone.

like image 156
Nikolay K Avatar answered Sep 27 '22 20:09

Nikolay K