Problems with access rights for Java and C + +

Source: Internet
Author: User

Transferred from Network: http://blog.sina.com.cn/s/blog_4bc954d30100wtxe.html

The understanding of permissions is important to understand what is visible and what is not seen

First declare: Java, friendly this modifier does not have an explicit declaration, in the member variables and methods before the modifier is not used, the default is friendly.

Java scope Public , private,protected , and no-write differences
Before I explain these four keywords, I want to make a simple definition of the relationship between classes, to inherit their class can think they are their own children, and for a directory of their own class, think of their own friends.
1, public:public indicates that the data member, the member function is open to all users, all users can directly make the call
2, private:private means private, private means that except class himself, no one can directly use, private property sacred inviolability, even children, friends, can not be used.
3, protected:protected for children, friends, is public, free to use, without any restrictions, and for other external class,protected become private.
Scope Current class same package descendant class other package

Public√√√√

Protected√√√x

Friendly√√xx

Private√xxx
In order to be clear, in three different situations to summarize.
An access-rights modifier modifies member variables and methods
Public: Indicates that the member variable and method are common and can be accessed under any circumstances.
Protected: Must be in the same package to be accessed. (It's easier said than not, see an example to understand)
Eg:class A
{
protected int weight;
protected int f (int a,int b)
{
Method body
}
}
Assuming that B and a are in the same package, then
Class B
{
void G ()
{
A a=new a ();
a.weight=100;//Legal
A.F (3,4); Legal
}
}

Friendly: In this case, the same as protected. The difference is in the second and third cases.
Eg:class A
{
int weight;
int f (int a,int b)
{
Method body
}
}
Assuming that B and a are in the same package, then
Class B
{
void G ()
{
A a=new a ();
a.weight=100;//Legal
A.F (3,4); Legal
}
}
Private: Accessible only 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; Legal
int M=te.getmoney (); Legal
System.out.println ("money=" +m);
}
}
PS: In fact, to modify the important data into private, and then write a public function to access it, just reflect the characteristics of OOP, is the embodiment of OOP security.

Two-access-modifier-modifier-Class
1, the class cannot be decorated with protected and private .
2, a class called a friendly class that is decorated with friendly, is guaranteed to be in the same package when creating objects using friendly classes in another class .

Three access rights modifiers and inheritance
The access modifier here refers to the decorated member variables and methods. Can be divided into two situations:
1,subclasses are in the same package as the parent class
Only variables and methods declared as private cannot be inherited (accessed) at this time.
eg
Class Father
{
private int money;
int weight=100;

}
Class Son extends Father
{
Viod F ()
{
money=10000;//illegal
weight=100; Legal
}
}
2, the subclass is not in the same package as the parent class
Both private and friendly cannot be inherited (accessed), protected and public can.
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;//Legal
height=170;//illegal, height is friendly modified variable
SYSTEM.OUT.PRINTLN (money);//Output is 10000
Setmoney (300); Illegal
int Number=getmoney (); Legal
SYSTEM.OUT.PRINTLN (number);//output result is 10000
}
public static void Main (String args[])
{
Son sss=new son ();
SSS.F ();
}
}
Therefore, the access permission modifier permissions from high to low are public, protected, friendly, private.

In C + +

Since there is no concept of a package, protected differs from Java protected in that the protected in Java is not only accessible to children, but also to other parts of the package that can be displayed through child objects, such as

Package Pack1

public class a{

protected int A;

}

Package Pack1

public class b{

public static void Main (string[] args) {

System.out.println (a);

}

}

The above code is not a problem, but it cannot be accessed if B is not in the same package as a. Therefore, in Java, protected in the visibility is similar to the packaged permission, but only in the inheritance, there is no demarcation to the package, packaged permission does not allow the child to access the properties of the package access of the parent object within the other package.

In C + +, the protected attribute has a strict definition, that is, only the vertical access permission, only its own descendants can access, limit the protected property of a class, only its internal and sub-object can be accessed inside, and in other places is not visible, in order to illustrate this problem, see the following code:

Class a{
Protected
int A;
Protected
void print () {
cout<< "A protected method" <<endl;
}
};
Class B:public a{
Protected
void Test () {
cout<< "Test A" <<endl;
}
};
int main () {
b b;
B.test ();
B.print ();
}

The above code is not compiled, because B.test () is not visible in main, and main cannot access the protected method of B, even if the method is called through a child object, if the protected method in B is changed to public, then the code can, compile successfully, After the compilation succeeds, the protected property of the parent class can be asked in B.

The following table lists the three types of inheritance access to properties for C + +:

Public inheritance protected inherit private inheritance

Public Property public protected private

Protected property protected protected private

Private property is not visible invisible invisible

Problems with access rights for Java and C + +

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.