Explanation of public, private, and protected access permissions in Java, C ++

Source: Internet
Author: User

In Java:

The access permissions in Java include public, private, protected, and default package access permissions. If the attribute methods in the class do not show the specified access permission, the package access permission is granted, I like to call it packeged permission. Many people also call it friendly access permission, while packaged and friendly do not actually exist.

For a detailed explanation, I will introduce a blog of others to explain:

Java access permission modifier public protected friendly private usage Summary

First, declare: in java, the friendly modifier is not explicitly declared, and there is no modifier before the member variables and methods. The default modifier is friendly.
In order to be clear, we can summarize three different situations.

1. access permission modifier to modify member variables and Methods
Public: indicates that the member variables and methods are common and can be accessed under any circumstances.

Protected: must be in the same package to be accessed. (This is simple. You can see the example here)
Eg: class
{
Protected int weight;
Protected int f (int a, int B)
{
// Method body
}
}
Assume that B and A are in the same package
Class B
{
Void g ()
{
A a = new ();
A. weight = 100; // valid
A. f (3, 4); // valid
}
}
Note: what is in the same package?
A: The class packaged with package is called in the same package. (I don't understand. For example)
Eg: in JDK src \ java \ io, you will see many java classes. The first source code is package java. io;
If a package is not used, classes in the same directory are considered as the same package.

Friendly: in this case, it is the same as protected. The difference is in the second and third cases.
Eg: class
{
Int weight;
Int f (int a, int B)
{
// Method body
}
}
Assume that B and A are in the same package
Class B
{
Void g ()
{
A a = new ();
A. weight = 100; // valid
A. f (3, 4); // valid
}
}
Private: It can only be accessed in this class.
Eg: class Test
{
Private int money;
Test ()
{
Money = 2000;
}
Private int getMoney ()
{
Return money;
}
Public static void main (String args [])
{
Test te = new Test ();
Te. money = 3000; // valid
Int m = te. getMoney (); // valid
System. out. println ("money =" + m );
}
}

PS: Actually, modifying important data to private and then writing a public function to access it reflects the encapsulation feature of OOP and the security of OOP.

2. access permission modifier class
1. You cannot use protected or private to modify the class.
2. Classes modified with friendly are called friendly classes. When using friendly classes in another class to create objects, make sure they are in the same package.

3. access permission modifier and inheritance
The access modifier here refers to modifying member variables and methods. There are two scenarios:
1. subclass and parent class are in the same package
At this time, only variables and Methods declared as private cannot be inherited (accessed ).
Eg:
Class Father
{
Private int money;
Int weight = 100;

}
Class Son extends Father
{
Viod f ()
{
Money = 10000; // invalid
Weight = 100; // valid
}
}
2. The Child class and parent class are not in the same package.
In this case, neither private nor friendly can be inherited (accessed), and protected and public can be used.
Eg:
Father. java

Package com. aaa
Public class Father
{
Int height;
Protected int money = 120;
Public int weight;
Protected int getMoney ()
{
Return money;
}
Void setMoney (int newMoney)
{
Money = newMoney;
}
}

Son. java
Package com. bbb
Import com. aaa. Father;
Public class Son extends Father
{
Void f ()
{
Money = 10000; // valid
// Height = 170; // invalid. The height is a variable modified by friendly.
System. out. println (money); // The output result is 10000.
// SetMoney (300); // invalid
Int number = getMoney (); // valid
System. out. println (number); // The output result is 10000.
}
Public static void main (String args [])
{
Son sss = new Son ();
Sss. f ();
}
}
Therefore, the access permission modifiers are listed as public, protected, friendly, and private.

 

C ++

Because the Package concept does not exist, protected is different from protected in java. protected in Java is not only accessible to sub-objects, but also visible elsewhere in the package through sub-object calls, as shown in figure

Package pack1

Public class {

Protected int;

}

Package pack1

Public class B {

Public static void main (String [] args ){

System. out. println ();

}

}

The above code is correct, but if B and A are not in the same package, they cannot be accessed. Therefore, in Java, the visibility of protected is similar to that of packaged, but in inheritance, it does not define packets, the packaged permission does not allow the sub-object to access the attributes of the package access permission of the parent object in other packages.

In C ++, the protected attribute is strictly defined, that is, it only has the vertical access permission, and can only be accessed by itself and its descendants. Only the protected attribute of a class is limited, it can only be accessed inside and inside the sub-object, but is invisible elsewhere. To illustrate this problem, see the following code:

Class {
Protected:
Int;
Protected:
Void print (){
Cout <"A protected method" <endl;
}
};
Class B: public {
Protected:
Void test (){
Cout <"test A" <endl;
}
};
Int main (){
B B;
B. test ();
// B. print ();
}

The above Code cannot be compiled because B. test () is invisible in main, and main cannot access the protected method of B. Even if the method is called through sub-objects, if the protected method in B is changed to public, the code can be compiled successfully. After the compilation is successful, you can ask the protected attribute of the parent class in B.

The following table lists the access to the attributes of C ++ inheritance:

Public attribute protected attribute private attribute

Public inherits from public protected and is invisible.

Protected inherits protected which is invisible

Private inherits private invisible

In Java:

The access permissions in Java include public, private, protected, and default package access permissions. If the attribute methods in the class do not show the specified access permission, the package access permission is granted, I like to call it packeged permission. Many people also call it friendly access permission, while packaged and friendly do not actually exist.

For a detailed explanation, I will introduce a blog of others to explain:

Java access permission modifier public protected friendly private usage Summary

First, declare: in java, the friendly modifier is not explicitly declared, and there is no modifier before the member variables and methods. The default modifier is friendly.
In order to be clear, we can summarize three different situations.

1. access permission modifier to modify member variables and Methods
Public: indicates that the member variables and methods are common and can be accessed under any circumstances.

Protected: must be in the same package to be accessed. (This is simple. You can see the example here)
Eg: class
{
Protected int weight;
Protected int f (int a, int B)
{
// Method body
}
}
Assume that B and A are in the same package
Class B
{
Void g ()
{
A a = new ();
A. weight = 100; // valid
A. f (3, 4); // valid
}
}
Note: what is in the same package?
A: The class packaged with package is called in the same package. (I don't understand. For example)
Eg: in JDK src \ java \ io, you will see many java classes. The first source code is package java. io;
If a package is not used, classes in the same directory are considered as the same package.

Friendly: in this case, it is the same as protected. The difference is in the second and third cases.
Eg: class
{
Int weight;
Int f (int a, int B)
{
// Method body
}
}
Assume that B and A are in the same package
Class B
{
Void g ()
{
A a = new ();
A. weight = 100; // valid
A. f (3, 4); // valid
}
}
Private: It can only be accessed in this class.
Eg: class Test
{
Private int money;
Test ()
{
Money = 2000;
}
Private int getMoney ()
{
Return money;
}
Public static void main (String args [])
{
Test te = new Test ();
Te. money = 3000; // valid
Int m = te. getMoney (); // valid
System. out. println ("money =" + m );
}
}

PS: Actually, modifying important data to private and then writing a public function to access it reflects the encapsulation feature of OOP and the security of OOP.

2. access permission modifier class
1. You cannot use protected or private to modify the class.
2. Classes modified with friendly are called friendly classes. When using friendly classes in another class to create objects, make sure they are in the same package.

3. access permission modifier and inheritance
The access modifier here refers to modifying member variables and methods. There are two scenarios:
1. subclass and parent class are in the same package
At this time, only variables and Methods declared as private cannot be inherited (accessed ).
Eg:
Class Father
{
Private int money;
Int weight = 100;

}
Class Son extends Father
{
Viod f ()
{
Money = 10000; // invalid
Weight = 100; // valid
}
}
2. The Child class and parent class are not in the same package.
In this case, neither private nor friendly can be inherited (accessed), and protected and public can be used.
Eg:
Father. java

Package com. aaa
Public class Father
{
Int height;
Protected int money = 120;
Public int weight;
Protected int getMoney ()
{
Return money;
}
Void setMoney (int newMoney)
{
Money = newMoney;
}
}

Son. java
Package com. bbb
Import com. aaa. Father;
Public class Son extends Father
{
Void f ()
{
Money = 10000; // valid
// Height = 170; // invalid. The height is a variable modified by friendly.
System. out. println (money); // The output result is 10000.
// SetMoney (300); // invalid
Int number = getMoney (); // valid
System. out. println (number); // The output result is 10000.
}
Public static void main (String args [])
{
Son sss = new Son ();
Sss. f ();
}
}
Therefore, the access permission modifiers are listed as public, protected, friendly, and private.

 

C ++

Because the Package concept does not exist, protected is different from protected in java. protected in Java is not only accessible to sub-objects, but also visible elsewhere in the package through sub-object calls, as shown in figure

Package pack1

Public class {

Protected int;

}

Package pack1

Public class B {

Public static void main (String [] args ){

System. out. println ();

}

}

The above code is correct, but if B and A are not in the same package, they cannot be accessed. Therefore, in Java, the visibility of protected is similar to that of packaged, but in inheritance, it does not define packets, the packaged permission does not allow the sub-object to access the attributes of the package access permission of the parent object in other packages.

In C ++, the protected attribute is strictly defined, that is, it only has the vertical access permission, and can only be accessed by itself and its descendants. Only the protected attribute of a class is limited, it can only be accessed inside and inside the sub-object, but is invisible elsewhere. To illustrate this problem, see the following code:

Class {
Protected:
Int;
Protected:
Void print (){
Cout <"A protected method" <endl;
}
};
Class B: public {
Protected:
Void test (){
Cout <"test A" <endl;
}
};
Int main (){
B B;
B. test ();
// B. print ();
}

The above Code cannot be compiled because B. test () is invisible in main, and main cannot access the protected method of B. Even if the method is called through sub-objects, if the protected method in B is changed to public, the code can be compiled successfully. After the compilation is successful, you can ask the protected attribute of the parent class in B.

The following table lists the access to the attributes of C ++ inheritance:

Public attribute protected attribute private attribute

Public inherits from public protected and is invisible.

Protected inherits protected which is invisible

Private inherits private invisible

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.