Java access permission Modifier

Source: Internet
Author: User

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)

 

Java code

 

 

Class

{

Protected int weight;

Protected int F (int A, int B)

{

// Method body

}

}

Assume that B and A are in the same package

 

Java code

 

 

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 srcjavaio, 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.

 

Java code

 

 

Class

{

Int weight;

Int F (int A, int B)

{

// Method body

}

}

Assume that B and A are in the same package

 

Java code

 

 

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.

 

Java code

 

 

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 ).

 

Java code

 

 

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.

 

Father. Java

 

Java code

 

 

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

 

Java code

 

 

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.

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.