質問

I know a subclass can not inherit from a final superclass but this subclass section of my book never indicates what would happen in the case of a nested class.

役に立ちましたか?

解決

No.

The whole point of making a class final is so that it cannot be inherited from.

See the last paragraph here.

他のヒント

No, the JVM sees the nested class as Outer$Inner and for binary compatibility will not try to extend a final class with it, inner or not. In fact, other than compile-time differences not generally preserved for runtime/link-time, it's not much more than a static inner class which is just a class with a $ in its name. The compiler simply catches it earlier.

Inner classes inside final classes are fine as no extend/subtype relationship is implied or given.

Try it and see. Your compiler will not beat you to death for an error. Its best property is its patience, even with its strict syntax. Checking for yourself would be faster than asking here.

I think I first misunderstood your question. To be thorough though:

Nested classes do not inherit from their parents so a final class could contain an inner class because every instance of the outer class would contain the inner class (unless static is used when declaring the inner class). There is no implicit inheritance in the relationship between the outer and inner classes.

Also, the inner class could not inherit from the outer class because the outer class is considered final and thus could never be extended.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top