Blocking in Java (official and unofficial)

zhaozj2021-02-16  49

Inheritance (official and unofficial measures) by USMAN Saleem in Java

In object-oriented theory, some programs require you to use one way to declare an unshantable class. In general, if the functionality provided should not be changed, or more appropriate, this will happen when it is overridden. In this article, I discussed the ways to achieve this behavior in the Java language, official and unofficial methods.

A keyword "Final" provided by the official approach Java language can be used to fulfill the task. Take a look at the source code sample: //finaldemo.javapublic final class finalDemo {} Let's make another class, which will inherit the class of the above declaration. The "Extends" keyword provided by the Java language will enable a class to inherit an existing class. //Final class finaldemo2 extends FinalDemo {} After compiling the first class, if you follow the second class, then the JDK compiler reports an error, you will get the following error message: FinalDemo2.java: 1: Cannot Inherit from Final FinalDemopublic Class Final Demo2 Extends FinalDemo {} ^ 1 Error Now, you have successfully blocked the first class from being inherited by another class. Unofficial Measures However, it is not unique to preventing the classes from being inherited by other classes. Considering the following code, I declapted the constructor as private, and also static method is 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 ();}} A modified form of the above code is also known as the "Singleton Pattern," WHERE The GetInstance Method ALWAYS RETURNS ONE INE Instance of the class. But why this code blocks the class from inheritage? Consider the following code, the admission should be able to inherit the above class. Public Class PrivateTest2 Extends PrivateTest {} After compiling the first class, if you follow the second class, then the JDK compiler reports an error, you will get the following error message: PrivateTest2.java: 1: privatetest () HAS Private Access in PrivateTestPublic Class PrivateTest2 Extends PrivateTest {^ 1 Error Second class cannot inherit the first class. But what is wrong? The Java language requires at least one component device (constructor) to be provided in a class. If you do not provide any member, JDK will insert a default member device in the class you declared. In other words, the default is a member that does not have parameters, an empty means, and a public access permission. However, if you define a member device yourself, the JDK compiler will not insert such a default component. We have just declare a default component in the PrivateTest class, but we will change the default public access to Private permissions, which are rules that meet the JDK compiler syntax check. Now let's take a look at the second department.

转载请注明原文地址:https://www.9cbs.com/read-24934.html

New Post(0)