I found a code that was like this.
public final class MiClase{
}
It's something I had not seen and I do not know if it's any good to put final before class
I found a code that was like this.
public final class MiClase{
}
It's something I had not seen and I do not know if it's any good to put final before class
A final class is simply a class that can not be inherited.
imagine that the whole class diagram of your software is a tree, and a final class would be the page, I leave you an image so you can understand better:
This does not mean that all references to objects in the class would act as if they were declared final.
It means that the class can not be extended. According to the Java specification :
8.1.1.2.
final
ClassesA class can be declared
final
if its definition is complete and no subclasses are desired or required.It is a compile-time error if the name of a final class appears in the
extends
clause (§8.1.4 ) of another class declaration; this implies that afinal
class can not have any subclasses.It is a compile-time error if a class is declared both
final
andabstract
, because the implementation of such a class could never be completed (§8.1.1.1 ).Because a
final
class never has any subclasses, the methods of afinal
class are never overridden (§8.4.8.1 ).
Translated:
8.1.1.2. Classes
final
isA class can be declared
final
if its definition is complete and they are not desired or require sub classes.It is a compilation error if the name of a class
final
appears in clauseextends
(§8.1.4 ) of another class declaration; this implies that a classfinal
can not have subclasses.It is a compilation error if a class is declared
final
andabstract
at the same time, because the implementation of that class could never be completed ( §8.1.1.1 ).Since a class
final
can never have subclasses, the methods of a classfinal
are never overwritten ( §8.4.8.1 ).
This means:
public final class NoMePuedenExtender {
public void noPuedesTocarme() {
System.out.println("intocable");
}
}
//la clase de abajo lanza un error de compilación
//puesto que las clases finales no pueden extenderse
//public class IntentareExtenderte extends NoMePuedenExtender { }
//la clase de abajo lanza error de compilación
//el modificador "final" indica que no se puede extender
//mientras que "abstract" indica que debe extenderse
//para poder tener instancias de esta clase
//esto es completamente ilógico...
//public final abstract class ExtiendemeSiPuedes { }
You can use this type of classes when you do not want another developer to be able to change the definition of the methods of your class. A clear example of this are the String
classes and the primitive wrappers Integer
, Long
, Double
, etc. Nobody would need to add or overwrite methods of these classes (this is how the Java authors designed these classes, it is not my opinion). If you want to do it, you can create utilitarian classes like StringUtils
and similar.
It is only necessary to mention that it is the use of declaring a class as final
.
Extending classes is very useful, and then something that happens constantly in the iterations of development.
However, there are cases where class inheritance can cause ambiguities in existing code.
For example, if I have code in which I have an immutable class MiClase
that only contains final values, and for the system it is important that this class never has mutable fields (for example to avoid mess with multiple threads), it can have sense of declaring class final
.
You can also declare methods such as final
, meaning that they can not be overwritten by a subclass. That is very important in the case of methods that are called from the constructor.