Home >Java >javaTutorial >A detailed introduction to the permission issues of inherited classes in Java

A detailed introduction to the permission issues of inherited classes in Java

王林
王林forward
2019-11-29 17:05:242324browse

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete