Summary of protected modifier usage in Java

Source: Internet
Author: User
Tags object control characters modifier variables variable access
1. Protected access control characters can be used for methods and member variables.
2. Methods and member variables declared as protected can be accessed by all classes in the same package, just like the default modifier package.
3. Can be accessed by subclasses of this class, subclasses can be not in a package with the parent class.
This way, when you want a method or member variable in a class to be visible in the package, and its subclasses can be accessed (subclasses may not be in the same package as the parent class) but do not want all classes to be able to access the class with the protected modifier.
Accessibility:
Public > Protected > Package >private
Attention:
4. But a subclass in another package can access the protected members of the Super-class via only the references of SUBCLA SS or its subclasses. A subclass in the same package doesn ' t have this restriction. This ensures is classes from packages are accessing only the members, are part of their inheritance.


The following examples illustrate these points: (Pay special attention to the 4th)
We will create a parent class Bird.java, placed in the Birdpack package, with a member variable nfeathers protected int in the parent class;
Then create 4 subclasses of the bird class Duck1.java,duck2.java,duck3.java,swan.java, put them in the Duckpack package and illustrate the above points by invoking different methods of nfeathers in each subclass


The following program is not intended to run because access control is determined during compilation, and we simply compile the following file to see if it passes. Before compiling the following files, think about whether you can compile and pass?

Bird.java------------------------------
Package birdpack;

public class bird{
protected int nfeathers;

}

Duck1.java-----------------------------
Package duckpack;

Import Birdpack. Bird;

public class Duck1 extends bird{
public void Setn (int duck1n) {
To access protected variables in a parent class directly in a subclass
nfeathers=duck1n;
}
}
Duck2.java------------------------------
Package duckpack;

Import Birdpack. Bird;

public class Duck2 extends bird{

public void construct (int newduck2) {
Duck2 D2 = new Duck2 ();
Accessing protected variables in a parent class in a subclass through an object in a subclass
D2.nfeathers=newduck2;
}
}

Duck3.java------------------------------
Package duckpack;

Import Birdpack. Bird;

public class Duck3 extends bird{

public void construct (int newduck3) {
Bird B = new Bird ();
The parent class object in the subclass cannot access the protected variable in the parent class
B.nfeathers=newduck3;
}
}

Swan.java--------------------------------
Package duckpack;

Import Birdpack. Bird;

public class Swan extends bird{

public void construct (int swan) {
Duck1 D1 = new Duck1 ();
The protected variable in the parent class cannot be accessed by a subclass with an object of another subclass
D1.nfeathers=swan;
}
}

Compile the above several files, the latter 2 cannot pass. Compiler hints:
"Nfeathers has protected access in Birdpack. Bird ".

Bird.java------------------------------
Package birdpack;

public class bird{
protected int nfeathers;

}

Duck1.java-----------------------------
Package duckpack;

Import Birdpack. Bird;

public class Duck1 extends bird{
public void Setn (int duck1n) {
To access protected variables in a parent class directly in a subclass
nfeathers=duck1n;
}
}
Duck2.java------------------------------
Package duckpack;

Import Birdpack. Bird;

public class Duck2 extends bird{

public void construct (int newduck2) {
Duck2 D2 = new Duck2 ();
Accessing protected variables in a parent class in a subclass through an object in a subclass
D2.nfeathers=newduck2;
}
}

Duck3.java------------------------------
Package duckpack;

Import Birdpack. Bird;

public class Duck3 extends bird{

public void construct (int newduck3) {
Bird B = new Bird ();
The parent class object in the subclass cannot access the protected variable in the parent class
B.nfeathers=newduck3;
}
}

Swan.java--------------------------------
Package duckpack;

Import Birdpack. Bird;

public class Swan extends bird{

public void construct (int swan) {
Duck1 D1 = new Duck1 ();
The protected variable in the parent class cannot be accessed by a subclass with an object of another subclass
D1.nfeathers=swan;
}
}

Compile the above several files, the latter 2 cannot pass. Compiler hints:
"Nfeathers has protected access in Birdpack. Bird ".

4th, even in subclasses, protected methods and member variables in the parent class can only be accessed through references to subclasses (or subclasses of subclasses).
In the Duck3 and Swan two subclasses, it is not possible to access protected methods and member variables in the parent class directly through the parent class and another subclass.

Attach: Compile Skill
When compiling a source file that contains a package declaration, using the-D option provides a lot of convenience, ensuring that the compiled class file is stored under the correct directory path.
We can put all of the above 5 files in the Protectedtest directory, and then use the-D option to compile:
Javac-d. Bird.java
Javac-d. Duck1.java
.....
Where. Represents the directory hierarchy in which the package structure is created in the current directory. After the compilation is successful, 2 directories will be generated under Protectedtest: Birdpack and Duckpack, which is the generated class file.




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.