The so-called access permission refers to whether the object can operate its own variables or call methods in the class through the "." operator. Access restriction modifiers include private, protected, and public, which are Java keywords used to modify member variables or methods.
Notice:
When writing a class, instance methods in the class can always operate instance variables and class variables in the class; class methods can always operate class variables in the class, regardless of access restrictions.
Private variables and private methods
Member variables and methods modified with the keyword private are called private variables and private methods.
For example:
classTom{privatefloatweight;//weight is a private float type variable privatefloatt(floata, floatb){//method t is a private method returna+b;}}
Notice:
When an object is created using class Tom in another class, the object cannot access its own private variables and call private methods in the class.
Shared variables and shared methods
Member variables and methods modified with the keyword public are called shared variables and methods.
For example:
classTom{publicfloatweight;//weight is a public float type variable publicfloatt(floata,floatb){//method t is a public method returna+b;}}
Notice:
When an object is created using class Tom in any class, the object can access its own public variables and call public methods in the class.
Protected member variables and protected methods
Member variables and methods modified with the keyword protected are called protected member variables and protected methods.
For example:
classTom{protectedfloatweight;//weight is a protected float type variable protectedfloatt(floata,floatb){//method t is a protected method returna+b;}}
Notice:
When an object is created using class Tom in another class, if this class is in the same package as the Tom class, then the object can access its own protected variables and call the protected method in the class.
Friendly variables and friendly methods
Member variables and methods that are not modified with the keywords private, public, and protected are called friendly variables and friendly methods.
For example:
classTom{floatweight;//weight is a friendly float type variable float(floata, floatb){//method t is a friendly method returna+b;}}
Notice:
When an object is created using class Tom in another class, if this class is in the same package as the Tom class, then the object can access its own friendly variables and call friendly methods in the class.
When an object is created using class Tom in another class, if this class is in the same package as the Tom class, then the object can access its own protected variables and call the protected method in the class.