Java access modifier and visibility

Source: Internet
Author: User

 

Access modifier and visibility

 

 

In reality, everyone has their own secrets. Java is an object-oriented programming language, and its main function is to describe existing things and phenomena in reality, java provides access modifiers to share and private information.

 

Java has three types of access modifiers and four types of visibility.

 

The three access modifiers are private, default, protected, and public.

 

Attributes and Methods declared as private can only be accessed in the current class.

 

The following example defines the sing method as private.

 

Public classDog {

 

Private voidSing (){

System.Out. Println ("Wang ...");

}

}

 

The entry functions are as follows:

Public classDogdemo {

 

Public static voidMain (string [] ARGs ){

Dog dog =NewDog ();

 

Dog. Sing ();

}

 

}

 

A compilation error occurs during the call:

 

Exception in thread "Main" Java. Lang. Error: unresolved compilation problem:

The method sing () from the type dog is not visible

 

 

The preceding instance description defines private attributes and methods.

 

Attributes and methods defined as default can be accessed in the current class or in the same package. Classes of different packages cannot be accessed, you do not need to add any access modifiers before defining the default attributes and methods.

 

The dog class is defined as follows:

PackageCom. ICSS. OA. pk0;

 

Public classDog {

 

PrivateString dogname;

PublicString getdogname (){

ReturnDogname;

}

 

Public voidSetdogname (string dogname ){

This. Dogname = dogname;

}

 

VoidSing (){

System.Out. Println ("Wang ...");

}

}

 

The call function is defined as follows:

PackageCom. ICSS. OA. pk0;

ImportCom. ICSS. OA. pk0.dog;

 

 

Public classDogdemo {

 

Public static voidMain (string [] ARGs ){

Dog dog =NewDog ();

 

Dog. Sing ();

}

 

}

 

The above dog and dogdemo are both located in pk0,

 

The running result is as follows: Wang...

 

If you place dog and dogdemo in different packages, a compilation error occurs. The example is as follows:

 

The dog class is defined as follows:

PackageCom. ICSS. OA. pk0;

 

Public classDog {

 

PrivateString dogname;

PublicString getdogname (){

ReturnDogname;

}

 

Public voidSetdogname (string dogname ){

This. Dogname = dogname;

}

 

VoidSing (){

System.Out. Println ("Wang ...");

}

}

Dogdemo is defined as follows:

PackageCom. ICSS. OA. PK1;

ImportCom. ICSS. OA. pk0.dog;

 

 

Public classDogdemo {

 

Public static voidMain (string [] ARGs ){

Dog dog =NewDog ();

 

Dog. Sing ();

}

 

}

 

Dog is located in pk0, and dogdemo is located in PK1

 

The compilation and running results are as follows:

Exception in thread "Main" Java. Lang. Error: unresolved compilation problem:

The method sing () from the type dog is not visible

 

From the above examples, we can see that the variables and attributes defined as default in different packages cannot be seen by the other party.

 

 

The visibility of procted and default is very similar, but protected can be accessed by sub-classes in different packages. First, let's look at an instance:

 

PackageCom. ICSS. OA. pk0;

 

Public classAnimal {

 

Protected voidSing (){

System.Out. Println ("in the animal sing method .");

}

}

 

Dog definition:

 

PackageCom. ICSS. OA. PK1;

 

ImportCom. ICSS. OA. pk0.animal;

 

Public classDogExtendsAnimal {

 

PrivateString dogname;

PublicString getdogname (){

ReturnDogname;

}

 

Public voidSetdogname (string dogname ){

This. Dogname = dogname;

}

 

Public voidF0 (){

Animal animal =NewAnimal ();

Animal. Sing ();

}

}

 

Entry Function Definition:

 

PackageCom. ICSS. OA. PK1;

 

ImportCom. ICSS. OA. pk0.animal;

 

 

Public classDogdemo {

 

Public static voidMain (string [] ARGs ){

Dog dog =NewDog ();

 

Dog. f0 ();

}

 

}

 

Note: Dog and animal are in different packages. Dog inherits from animal. We want to call the sing method in animal in the F0 of dog.

 

The compilation and running results are as follows:

 

Exception in thread "Main" Java. Lang. Error: unresolved compilation problem:

The method sing () from the type animal is not visible

At com. ICSS. OA. pk1.dog. f0 (Dog. Java: 21)

At com. ICSS. OA. pk1.dogdemo. Main (Dogdemo. Java: 12)

If the subclass wants to access the method declared as protected in the parent class, it must be inherited. Modify the above Code

The animal and dogdemo codes do not change. The dog code is as follows:

 

PackageCom. ICSS. OA. PK1;

 

ImportCom. ICSS. OA. pk0.animal;

 

Public classDogExtendsAnimal {

 

PrivateString dogname;

PublicString getdogname (){

ReturnDogname;

}

 

Public voidSetdogname (string dogname ){

This. Dogname = dogname;

}

 

Public voidF0 (){

Sing (); // note the call method here

}

}

 

The Calling Result of the entry function is as follows: In the animal sing method.

 

 

The logose isolation method defined as public can be accessed by other classes in any package.

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.