Java basics-attributes of classes in Java-general Linux technology-Linux programming and kernel information. For details, refer to the following section. Public, private, and protected display the attributes and service types of the three types. public is accessible at will, and private is inaccessible from the outside (showing data encapsulation) and protected indicate that the data is protected. The following is a detailed description of the differences between the three.
First, you need to understand the concept of the package. There are several classes that have a certain relationship with each other, so you can define a package to accommodate these classes. A package can not only contain classes, it can have classes, interfaces, components, nodes, and collaboration. The package can also contain packages.
Now let's discuss the differences between the three types.
Public classes and attributes can be arbitrarily called by the outside world, whether in the class or cross-class access (if they do not belong to the same package, the import statement must be called, add package name), which is quite understandable.
Private attributes and methods are the most selfish. The attributes and methods defined by Private can only be used in this class. They cannot be used in any way outside the class, for example:
Class Date
{
Private int day;
Public void tomorrow ()
{
This. day = this. day + 1;
}
}
Public class DateUser
{
Public static void main (String args [])
{
Date mydate = new Date ();
Mydate. day = 21; // note that this sentence is incorrect.
}
}
In this program, a Date Class Object mydate is created, which is acceptable. However, because the day in the Date class is a private variable, the created object mydate cannot pass mydate. day to access this property. If you want to use this private variable in other classes, you can only access this property through the public method in Date, the encapsulation of data is guaranteed. Just like the story of the newsstand mentioned in the target object, the people who buy newspapers and periodicals cannot reach out and take the newspaper or find the change, the same can be done only by the inner of the newsroom. In the above example, we can see that the default method (Date () in the Date class does not define its type, which means it is friendly, that means it is public in this package, while private in outsourcing. However, once the data type is defined as private, an error occurs even if Date mydate = new Date (). This is because the default builder has been defined as private, that is, even other classes in this package cannot be accessed.
For protected, we need to consider the concept of a package. If there are two classes, for example, Date and DateUser, which are respectively located in different packages, and DateUser is a class that inherits the Date class, if you want to allow the method in DateUser to access the method or attribute in Date, in addition to using import to import the package, you also need to consider the type of the method and attribute to be accessed. In general, for cross-package access, public methods and attributes must be accessible. However, if you change the methods and attributes to public, you can allow access to everything, security is not guaranteed. For this purpose, protected is added. If you define the methods and attributes to be accessed as protected, other classes cannot be accessed, the outsourced DateUser can access these attributes and methods through import, which ensures the security of data and the good use of data. Example:
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.