What are the final inner classes?

What does declaring a non-static inner class as final mean?

I tried a lot of links on google and stackoverflow.com , but they all seem to be dealing with inner classes that access the final members, and not the last inner classes. I found this link on Google, but even this does not explain.

Thanx in advance!

+6
source share
3 answers

There is no semantic difference between creating a final top-level class and creating an inner final class: it tells the compiler that you cannot inherit it. Final class classes are sometimes executed so that the compiler skips searching for the virtual table, but this is often seen as premature micro-optimization.

+5
source

It has the same semantics as the outer class declared final: the class cannot be extended.

Consider the following example:

 public class MyClass { public class A { } public class B extends A { } } 

If you add the last modifier to A, it will generate a compilation error.

0
source

Well, inner classes are no different from outer classes in this context. Thus, the following code is completely correct.

 class Outer { int some_member; class Inner { void method(); } } class OuterExtendsInner extends Outer.Inner{ } 

As we all know, the purpose of declaring a final class is that we forbid any external attacker from subclassing the class and using its capabilities in the same way as we can do with inner classes.

0
source

Source: https://habr.com/ru/post/918060/


All Articles