[Java] The difference between java scope public, private, protected and not written

http://yangmingjiayou.iteye.com/blog/151865  

This is an article I read elsewhere. I have seen similar articles before, and I thought a metaphor here was better, so I reprinted it. The link address is forgotten, tell me if you know, I will add it. Thank you! 


The difference between java scope public, private, protected and not written.     
Before explaining these four keywords, I want to make a simple definition of the relationship between classes. For inheriting their own classes, base classes can be considered as all My own children consider all classes in the same directory as my own friends. 



1. Public: public indicates that the data members and member functions are open to all users, and all users can directly call them. 



2. Private: private means private, which means that no one can directly except the class itself. Use, private property is sacred and inviolable, even children and friends cannot use it. 



3. Protected: For children and friends, protected is public and can be used freely without any restrictions. For other external classes, protected becomes private. 

Scope Current class Descendants of the same package Other packages 

public √ √ √ √ 

protected √ √ √ × 

friendly √ √ × × 

private √ × × ×If 

not written, the default is friendly

Guess you like

Origin blog.csdn.net/michellechouu/article/details/48751091