Summary of protected modifier in java

Source: Internet
Author: User
Tags modifier
1. The protected access controller can be used for methods and member variables.
2. The methods and member variables declared as protected can be accessed by all classes in the same package, just like the default modifier package.
3. The sub-class can be accessed by the sub-class of the class. The sub-class can be in a package other than the parent class.
In this way, when you want to make a method or member variable in a class visible in the package, and its subclass can also be accessed (the subclass may not be in the same package as the parent class) however, if you do not want all classes to access this class, you can use the protected modifier.
Public> protected> package> private
4. but a subclass in another package can access the protected members in the super-class via only the references of subclass or its subclasses. A subclass in the same package doesn' t have this restriction. this ensures that classes from other packages are accessing only the members that are part of their inheritance hierarchy.
The following example illustrates the preceding points: (pay special attention to the 4th points)
We will create a parent class Bird. java and place it in the birdpack package. The parent class has a member variable nFeathers of protected int;
Create four subclasses of the Bird class,,, and Swan. java, and place them in the duckpack. The preceding points are described by calling different nFeathers methods in each subclass.
The following program is not used for running, because access control requires confirmation during compilation. We only need to compile the following files to check whether the program can pass. Before compiling the following files, first think about whether the files can be compiled?
// Bird. java ------------------------------
Package birdpack;
Public class Bird {
Protected int nFeathers;
// -----------------------------
Package duckpack;
Import birdpack. Bird;
Public class Duck1 extends Bird {
Public void setn (int duck1n ){
// Directly access the protected variable in the parent class in the subclass
NFeathers = duck1n;
// ------------------------------
Package duckpack;
Import birdpack. Bird;
Public class Duck2 extends Bird {
Public void construct (int newduck2 ){

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