


A detailed introduction to the permission issues of inherited classes in Java
There is an issue that is easily overlooked in Java, and that is the relationship between the permissions of inherited classes and the permissions of base classes. Because when using inherited classes, you may rarely need to modify the access control character of the base class, but directly use the access control character of the base class.
If the base class has an attribute method that is private, can the subclass be modified to protected? If it is protected, can the subclass be modified to public or private? Let’s take a look at this issue next.
Related video tutorial recommendations: java teaching video
1. The base class method is private
First, if the base class The attribute method is private, so can the subclass be modified to protected or public? The answer is no. This answer can be concluded with a little reasoning. Since the attribute method of the base class is private, it is not visible to the subclass. Since the subclass is not visible, how can the permissions of the base class method be modified? What about the control symbol?
We can use a piece of code to verify this problem:
/*BaseClass.java*/ public class BaseClass { private void test() {} } /*ExtendClass.java*/ public class ExtendClass extends BaseClass { //@Override protected void test() {} }
If the annotations are removed from the above code, an error will be reported, indicating that the method cannot be found, because the method of the base class is private , there will be no problem if you remove the annotation, but at this time the test() method of the subclass and the test() method of the base class are two completely unrelated methods.
2. The base class method is friendly
If the base class method is friendly, which is the default permission, then there are two situations, one is the child The class and the base class are in the same package. One is that the subclass and the base class are not in the same package. When the subclass and the base class are in the same package, the permission control symbol can be expanded to protected or public:
/*BaseClass.java*/ package demo1; public class BaseClass { void test() {} } /*ExtendClass.java*/ package demo1; public class ExtendClass extends BaseClass { @Override protected void test() {} }
The above two classes are under the package demo1, and the code can be used correctly. But when the subclass and the base class are not in the same package, the situation is different:
/*BaseClass.java*/ package demo2.demo1; public class BaseClass { void test() {} } /*ExtendClass.java*/ package demo2; public class ExtendClass extends BaseClass { //@Override protected void test() {} }
If the subclass and the base class are not in the same package, then the default permissions cannot be extended. The reason is actually the same as private. The class cannot see the base class's methods, and therefore cannot extend the method's permissions.
3. The base class method is protected
If the base class method is protected, then the subclass can extend the access control character to public:
/*BaseClass.java*/ package demo2.demo1; public class BaseClass { protected void test() {} } /*ExtendClass.java*/ package demo2; public class ExtendClass extends BaseClass { @Override public void test() {} }
4. Summary
In fact, it is easy to figure out the extension situation. Remember the following guideline: Theoretically, subclasses are access control characters that can extend the base class. , but the access control character of the base class cannot be narrowed, and it can be expanded only when the subclass can see the base class method.
If you want to learn more related tutorials, you can visit: Java zero-based introduction
The above is the detailed content of A detailed introduction to the permission issues of inherited classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
