Java access modifier and understanding of the protected modifier

Source: Internet
Author: User
Tags modifier

2017-11-04 22:28:39

    • Permission to access the permission modifier

    • Access modifier protected Permissions Understanding

In core Java there is a phrase " in the object class, the Clone method is declared as protected, so anobject.clone () cannot be called directly. Subclasses can only invoke the protected clone method directly to clone itself. To do this, you must redefine the Clone method and declare it public so that all methods can clone the object . But don't all subclasses have access to protected methods ? Isn't every subclass a type of object? The following is an example of an issue with the protected access modifier.
1, create the Superclass.java file in Package1, there is a protected method:

    Package package1;            public class Superclass {          protected void method () {              System.out.println ("This is a protected method in the Super CLA SS. ");          }      }  

2. Create a Subclass1.java file in the same package, class SubClass1 and class SubClass2 inherit from superclass:

    Package package1;            public class SubClass1 extends superclass {public          static void Main (string[] args) {              superclass sup = new SUPERCL ();              SubClass1 sub1 = new SubClass1 ();              SubClass2 sub2 = new SubClass2 ();              Sup.method ();  Compile OK              Sub1.method ();  Compile OK              Sub2.method ();  Compile OK          }      }      class SubClass2 extends superclass{                }  

Compile all through, it is not difficult to understand that theprotected method is visible to the class in the same package .

3. If you create a Subclass1.java file in another package Package2, the content is the same as the Subclass1.java content in the package package1.

    Package package2;            Import Package1. Superclass;            public class SubClass1 extends superclass {public          static void Main (string[] args) {              SubClass1 sub1 = new Subclass ();              SubClass2 sub2 = new SubClass2 ();              Sub1.method ();  Compile OK              Sub2.method ();  Compile Error          }            }      class SubClass2 extends superclass{                }  

This way the compiler will report an error at Sub2.method () the method clone from the type superclass are not visiuable. In this case,both Sub1 and sub2 inherit from the parent class to the protected method called methods (), The Sub1.method () compilation passes, and the Sub2.method () compilation does not pass, Because Sub2 is another subclass of the parent class of the class in which he resides. from this we can think of: Cannot access another subclass in one subclass protected method, although these two subclasses inherit from the same parent class .

4 . If we rewrite the method of superclass in the SubClass2 class in the Package2 subclass1.class.

    Package package2;            Import Package1. Superclass;            public class SubClass1 extends superclass {public          static void Main (string[] args) {              SubClass1 sub1 = new Subclass 1 ();              SubClass2 sub2 = new SubClass2 ();              Sub1.method ();  Compile OK              Sub2.method ();  Compile OK          }            }      class SubClass2 extends superclass{          protected void Method () {              Super.method ( );          }      }  

This is similar to the first one, where the protected method can be accessed by a class in the same package. This protected method is visible to the SubClass1 class when the method () methods are overridden in the SubClass2 class, and the SubClass2 class and the SubClass1 class are under the same package . It should now be understandable that "to do this, you must redefine the Clone method and declare it as public so that all the methods can clone the object ."

5. If you call the protected method of the parent class object in the child class

    Package package2;            Import Package1. Superclass;            public class subclass extends superclass {public          static void Main (string[] args) {              superclass sup = new SUPERCL ();              Subclass sub = new subclass ();              Sup.method (); Compile Error              Sub.method ();  Compile OK          }            }  

Here I inherit the superclass from the other package in subclass, which has a protected method called methods () in this superclass. Call the method () of the instance sub of the subclass class in the subclass class, and the compilation will not error. And the same call superclass class instance Sup method () methods, compile Error!

Now it's important to be clear: class subclass does inherit the class superclass (including its method), so you can call your method in class subclass. But the protected method of class superclass is not visible to the subclass of its different buns.

Java access modifier and understanding of the protected modifier

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.