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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software