Public, private, protected shows the types of properties and services in the three classes. Public is accessible at will, and private is an outside world that cannot be accessed (displayed by data), and protected indicators are protected. Let's take a detailed explanation of these three differences.
Let's first understand the concept of a package. There are several classes, they have a certain relationship between them, then they can define these classes, including only classes, but can have classes, interfaces, components, nodes, collaboration, packs, can also be included. package.
Let us now discuss the differences between these three types.
As the class and attributes of the public, it can be called by the outside, whether in the class, or accesses accesses across the class (if you don't belong to the same package, you want to call the import statement, add the package name), this is very well understood. of.
Private's properties and methods are the most selfish, and its defined properties and methods can only be used in this class, and they cannot be used anyway, such as:
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 this sentence is wrong
}
}
In this program, you created a Date class object MyDate, which is ok, but because the DAY in the class is private variable, the created object MyDate cannot access this property via MyDate.day, if otherwise This private variable is used in the class, so you can only access this property through the method of public in Date, so that the data package is guaranteed, just like the story of the newsletter mentioned in the object. People who purchase newspapers must not reach themselves to take newspapers or find changes, but can only be handled by internal employers. In the above example, everyone can find that the default method in class DATE (Date ()) does not define its type, which means it is friendly, means it is public in this package, And in the outsourcing is private. But once the type is defined as Private, then Date MyDate = New Date () will also be wrong because the default builder has been defined as a private type, that is, even if other classes in this package cannot be accessed. .
For protected, considering the concept of the package, if there are two classes, such as: Date and Dateuser are located in different packages, and DateUser is a class that inherits the Date class, then if you want to access the method in Dateuser The method or attribute in Date, in addition to importing package, you need to consider what type of method and attribute to be accessed, in general, cross-package access, Public method and attributes must be accessed, but consider if If you need to use the methods and properties to be modified into public, then you can allow anything to access, you don't have security. For this consideration, you add protected, if you need access, you will be defined as protected, then Other classes cannot be accessed, and the outsourcing DateUser can access these properties and methods through IMPORT, that is, the security of the data is guaranteed to be used well. Examples are as follows: package a.b;
Public Class Sample
{
Protected void doing ();
}
Import a.b. *;
Public Class Sample1 Extends Sample
{
Public static void main (string args [])
{
Sample1 x = new Sample1 ();
x.doing ();
}
}