Official measures
A keyword "FINAL" provided by the JAVA language can be used to perform this task. Take a look at the following source code example:
The code copy is as follows:
// FinalDemo.java
public final class FinalDemo {
}
Let's make another class below, which will inherit the class declared above. The "EXTENDS" keyword provided by the JAVA language will enable a class to inherit from an existing class.
The code copy is as follows:
// FinalDemo2.java
public class FinalDemo2 extends FinalDemo {
}
After compiling the first class, if you then compile the second class, the JDK compiler reports an error and you will get the following error message:
FinalDemo2.java:1: cannot inherit from final FinalDemo
public class FinalDemo2 extends FinalDemo{}
^
1 error
Now, you have successfully prevented the first class from being inherited by another class through official methods.
Unofficial Measures
However, the way to prevent classes from being inherited by other classes is not unique. Considering the following code, I declare that the constructor is private, and also declare a static method to return a class object.
The code copy is as follows:
public class PrivateTest{
private PrivateTest(){
System.out.println("Private Default Constructor");
}
public static PrivateTest getInstance(){
return new PrivateTest();
}
}
The code modified above is called "Singleton pattern," a getInstance method always returns only one instance of this class. But why does this code prevent the class from being inherited? Consider the following code, the declared class should inherit the above class.
public class PrivateTest2 extends PrivateTest{
}
After compiling the first class, if you then compile the second class, the JDK compiler reports an error and you will get the following error message:
PrivateTest2.java:1: PrivateTest() has private access in PrivateTest
public class PrivateTest2 extends PrivateTest{
^
1 error
The second class cannot inherit the first class. But what does the error mean? The JAVA language requires that at least one component method be provided in a class. If you do not provide any artifact methods, the JDK will insert a default artifact method into the class you declared. In other words, the default is a component method without parameters, empty component body, and a public access permission. However, if you define a component method yourself, the JDK compiler will not insert such a default component method. We just declared a default component method in the PrivateTest class, but we changed the default public access permission to private permission, which are in line with the rules of JDK compiler syntax checking.
Now let’s take a look at the second department. The JAVA language also requires you to call the super class's component method on the first line of the component method. This is necessary to start inheritance features. In JAVA, we complete this task by calling the super() method, which will be mapped to the component method of a superclass. If you do not provide a default constructor for the superclass, the JDK compiler will insert a default superclass component method to call.
We just declared the constructor as a private permission in the first class. Now, when we inherit this class from other classes, the compiler will try to call a default superclass component method. Because component methods within the superclass scope are declared as private permission, the compiler will report an error saying that the superclass component methods cannot be called. Therefore, we prevent one class from being inherited by other classes through unofficial methods.
Usman Saleem
Mohammad Ali Jinnah University
E-mail: [email protected]