Java member variables and public methods

Source: Internet
Author: User

In the process of learning Java, I often use public, private, and protected for writing classes. My basic experience is that the member variables of a general class use private and public for the method, the method used inside the class uses protected. If a subclass exists, I will change the member variable in the parent class to protected. (The rest depends on the actual situation)

However, in some corporate pen questions, they will require you to write specific scopes of access qualifiers such as public. In fact, I usually did not go to the system to consider the scope of these access delimiters, especially outside the package, OK, the written test is not good.

This is the basic knowledge of Java, which is also valued by the company. I can't remember anything in my head, so I can only write these things down to help myself learn new things.

Class access permission table
Member permission This class Subclass Package External
Public

Protected

-

Private

-

-

-

Default (none)

-

-

It seems that the default value (none) is friendly.

Note: You can set the default value, that is, without any access qualifier, to "package access". You cannot directly access it in the subclass, but the classes in the same package can access it.

If you don't know the table above, or you haven't read the book for a long time, and forget the meaning of the table above, the following example is to help you learn new things.

For example, there is a class P in the demo package, which has a private member variable data, a public default constructor, a public access method getdata (), and a protected modification method setdata (), there is also an auxiliary method print () for the default access method ().

Package demo;

Public class P {

Private int data;

Public p (){

Setdata (0 );

}

Public int getdata (){

Return data;

}

Protected void setdata (INT v ){

Data = V;

}

Void print (){

System. Out. println ("used! ")

}

}

This class does not need to be said. In this class, no matter what access qualifier it is.

If the class Q extends P, you can call the default constructor of P, the access method getdata (), and the modification method setdata (). However, Q class cannot directly access the member variable data of P, and the auxiliary method print () does not work either (you can set the default value, that is, no access qualifier is added, which is interpreted as "Packet Access ", the class in the same package can be accessed ).

Import demo. p

Public class Q extends P {

Public Q (){

Super ();<--------------------------- Q can access the default constructor of the parent class.

}

Public Q (INT v ){

Setdata (v );<------------------------ Q can access the protected method of the parent class

}

Public String tostring (){

Int v = getdata ();<-------------------- Q can access the public method of the parent class

Return string. valueof (v );

}

Public void invalid1 (){

Data = 12;<--------------------------- Q cannot directly access the private data domain of the parent class

}

Public void invalid2 (){

Print ();<------------------------------- Q cannot directly access the default permission of the parent class.

}

}

For default access, only two classes in the same package can access the default access permission members of the other party. Therefore, because class Q is not part of the package demo, it cannot access the auxiliary method print () of P ().

Now consider the class R in the package demo. The r method can call the default constructor and access method getdata () of public in P, the modify method setdata () of protected, and the auxiliary method print () of the default access permission (). However, class R cannot directly access the private member variable data of P.

Package demo;

Public class r {

Private P;

Public R (){

P = new P ();<-------------------------- R can access the default constructor of P.

}

Public void set (INT v ){

P. setdata (v );<--------------------------- R can access the protected method of P.

}

Public int get (){

Return P. getdata ();<--------------------- R can access the public method of P.

}

Public void use (){

P. Print ();<------------------------------- R can access the default permissions of P.

}

Public void invalid1 (){

P. Data = 12;<------------------------- R cannot directly access the private data field of P.

}

}

The following describes the class S, which is neither part of the demo package nor directly or indirectly extended from class p. Therefore, class S can only call the default constructor and access method getdata () of the public access permission in P ().

Import demo. P;

Public class S {

Private P;

Public S (){

P = new P ();<------------------------------- S can access the default constructor of the public of P.

}

Public int get (){

Return P. getdata ();<------------------------ S can access the public method of P

}

Public void invalid1 (INT v ){

P. setdata (v );<------------------------------ S cannot access P's protected Method

}

Public void invalid2 (){

P. Data = 12;<------------------------------- S cannot directly access the private data domain of P.

}

Public void invalid3 (){

P. Print ();<------------------------------- S. The default permission of P cannot be directly accessed.

}

}

The above three classes correspond to three situations: Scope subclass, package, and external.

 

 

 

 

 

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.