
Abstract and Final class simultaneously in the Java programming language ?!
The article presents the author’s understanding of the chapter from the book Effective Java, Second Edition by Joshua Bloch In
simple language we define the concept of abstract and final class:
An abstract class cannot be instantiated, but it can have subclasses.
The final class is a class from which it is forbidden to be inherited.
For a more precise definition, refer to the official tutorial:
The combination of abstract and final for a class means that the class cannot have descendants and you cannot create an instance for this class.
For example, we need to make the abstract and final class at the same time, when we go to create a utility class consisting of static methods (for example, java.lang.Math or java.util.Arrays).
As you know, the default constructor is created only if the class does not contain any explicit constructors. Therefore, by defining a private constructor without parameters for the class, we will ensure that it is not allowed to be instantiated from code outside the class. AssertionError is not strictly required, but it provides insurance in case the constructor is accidentally called from a class. This ensures that the class will not be instantiated under any circumstances (abstract). This idiom also prevents subclassing from this class (final). All constructors must call the constructor of the superclass explicitly or implicitly, and subclasses of this class will not have access to the constructor of the base class.
simple language we define the concept of abstract and final class:
An abstract class cannot be instantiated, but it can have subclasses.
The final class is a class from which it is forbidden to be inherited.
For a more precise definition, refer to the official tutorial:
The combination of abstract and final for a class means that the class cannot have descendants and you cannot create an instance for this class.
For example, we need to make the abstract and final class at the same time, when we go to create a utility class consisting of static methods (for example, java.lang.Math or java.util.Arrays).
Decision
public class UtilityClass {
private UtilityClass() {
throw new AssertionError();
}
... // реализация статических методов
}
* This source code was highlighted with Source Code Highlighter.
As you know, the default constructor is created only if the class does not contain any explicit constructors. Therefore, by defining a private constructor without parameters for the class, we will ensure that it is not allowed to be instantiated from code outside the class. AssertionError is not strictly required, but it provides insurance in case the constructor is accidentally called from a class. This ensures that the class will not be instantiated under any circumstances (abstract). This idiom also prevents subclassing from this class (final). All constructors must call the constructor of the superclass explicitly or implicitly, and subclasses of this class will not have access to the constructor of the base class.