JAVA prevents classes from inheriting of official and unofficial

  • 2020-04-01 02:47:37
  • OfStack

The official way

The JAVA language provides a keyword "FINAL" that can be used to perform this task. Take a look at the following source code example:


//FinalDemo.java 
public final class FinalDemo { 
} 

      Now let's make another class that will inherit the class declared above. The "EXTENDS" keyword provided by the JAVA language will enable a class to inherit from an existing class.


//FinalDemo2.java 
public class FinalDemo2 extends FinalDemo { 
}   

After compiling the first class, if you then compile the second class, the JDK compiler reports the following error message:

Finaldemo2. Java :1: cannot inherit from final FinalDemo
Public class FinalDemo2 extends FinalDemo{}
                                                              ^
1 the error


Now you have succeeded in preventing the first class from being inherited by another class by official means.

Unofficial method

      However, preventing classes from being inherited from other classes is not unique. Consider the following code, which declares the constructor to be private and also declares a static method to return a class object.


public class PrivateTest{ 
        private PrivateTest(){ 
                System.out.println("Private Default Constructor"); 
        } 
        public static PrivateTest getInstance(){ 
                return new PrivateTest(); 
        } 
} 

      The code modified above is called the "Singleton pattern," and a getInstance method always returns only one instance of the class. But why does this code prevent classes from being inherited? Considering the following code, the declared class should be able to inherit from the above class.

Public class PrivateTest2 extends PrivateTest{


}


After compiling the first class, if you then compile the second class, the JDK compiler reports the following error message:

Privatetest2.java :1: PrivateTest() has private access in PrivateTest
Public class PrivateTest2 extends PrivateTest{
            ^
1 the error


      The second class cannot inherit from the first class. But what does it mean to prompt an error? The JAVA language requires that at least one component method be provided in a class. If you do not provide any component methods, the JDK will insert a default component method in the class you declare. In other words, the default is a component method with no parameters, an empty component, and a public access. 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 to private, which is consistent with JDK compiler syntax checking.  

      Now let's look at the second department. The JAVA language also requires that you call the component method of the call super class from the first line of the component method. This is necessary to start the inheritance feature. In JAVA, we do this by calling the method super(), which maps to a superclass's component method. If you do not provide a default constructor for the superclass, the JDK compiler will insert a default superclass component method for invocation.

      We just declared the constructor private in the first class. Now, when we inherit this class in another class, the compiler will attempt to call a default superclass component method. Because the superclass-scoped component methods are declared private, the compiler will report an error that the superclass component methods cannot be called. Therefore, we have unofficially prevented one class from being inherited by another.

Usman Saleem
Mohammad Ali Jinnah University
E - mail: usman_saleem@yahoo.com  


Related articles: