Differences between public protected private in Java

Source: Internet
Author: User

Java provides three access control permissions: Public protected private. Therefore, the access permission with the name "anonymous" means that external users cannot access the database. When inheriting the database, you must consider whether the database can be inherited.

PRIVATE: the access permission of the current class. If a member of the class is modified by private, the member can only be accessed and called in the class, but cannot be accessed from outside, obviously, using private can better hide data or methods. In inheritance, if the parent class has the private access permission, the subclass cannot access the method, and the method cannot be inherited or overwritten, if the subclass defines a method that is exactly the same as the parent class, this method is not overwritten, but a new method is defined in the subclass.

Protected: This access permission is not very understandable to many beginners. Members modified with protected can be accessed in this class, other classes or objects in the same package, and sub-classes in different packages. Generally, protected is used to override this method by subclass.

Public: You should be quite familiar with this. The maximum access permission is allowed.

Example:

Write two classes of TES in package one, vtest

 

Public classtest {

Public void Info (){

System. Out. println ("Info () of test is executed here ()");

}

Protected void infotest (){

System. Out. println ("test protection member test ");

}

}

Public classvtest extends test {

 

 

Public void Info (){

Super.info ();

System. Out. println ("the info () function of vtest is executed here ");

}

Public static void main (string [] ARGs ){

Vtest T = new vtest ();

T.info ();

T. infotest ();

 

System. Out. println ("***************************");

Test T1 = new test ();

T1.info ();

T1.infotest ();

}

Re-execution result:

Here, the info () of test is executed ()

The info () function of vtest is executed here.

Test protection member Testing

***************************

Here, the info () of test is executed ()

Test protection member Testing

 

As a result, both the test object and the inherited subclass object in the same package can access the protected member. Likewise, you can verify that different steamed stuffed bun classes and other classes cannot access the members modified by protected.

 

From the inheritance perspective, the members modified by protected can be inherited. From the preceding running results, vtest inherits the infotest () method of the parent class test, which can be seen from the running results. For better understanding, we create a new package named two and create a new class ftest.

Public classftest extends test {

 

Public static void main (string [] ARGs ){

Ftest T = new ftest ();

T. infotest ();

}

 

}

Running result:

Test protection member Testing

 

The sub-class override parent class method has the following rule: the access permission of the sub-class method should be higher than that of the parent class. This rule is customized for protected and private cannot be inherited, public has reached the maximum permission, and only the protected permission can be expanded. Modify the ftest class and override infotest ()

Public classftest extends test {

 

Public void infotest (){

Super. infotest ();

System. Out. println ("ftest overwrites infotest ()");

}

Public static void main (string [] ARGs ){

Ftest T = new ftest ();

T. infotest ();

}

The access permission of infotest () in the parent class is protected, and the permission in the subclass is extended to public. Running result:

 

Test protection member Testing

Infotest () is rewritten in ftest ()

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.