Witscale Test Center

3.2 Modifiers for access control > 3.2.3 The protected modifier


3.2.3 The protected modifier

The protected modifier is applicable only to methods and member variables. Similar to the private modifier, you cannot apply protected modifier to a top-level class. In other words, a top-level class cannot be declared as protected. The member variables and methods of a class, declared as protected are accessible to all the subclasses of the declaring class. It does not matter if the subclass is defined in different package. In addition, the protected member variables and methods are also accessible to all the classes in the same package as the class.

Listing 3.4, demonstrates the use of protected modifier with two classes, Cat and BobCat. Each class belongs to a different package. The Cat class declares a protected member variable, secretAboutCat.

 

 

Since class BobCat is a subclass of Cat class, it can access the protected member secretAboutCat. It does not matter that if it belongs to same package or different package.

Inheritance and protected modifier

The most interesting thing about protected modifier is that it is more than just an access modifier. It respects inheritance while granting the access. In simple words, when it is said that a protected member is accessible to all subclasses of the class, what it really means is that this protected member is inherited by the subclasses and as a consequence, it is accessible to the subclass. Listing 3.5 illustrates how the BobCat class can access inherited protected variable secretAboutCat. The BobCat class is a subclass of Cat, hence it does not matter that it belongs to different package while accessing the protected members of Cat.


 

You might notice another interesting thing about the protected modifier in method tryingToAccess2(). The variable newCat.secretAboutCat is not accessible to BobCat because the current BobCat instance does not inherit it from its super-class. Therefore, the bottom-line is that protected members (methods and variables) are accessible from the subclass only when they are actually inherited from the superclass.

 

The subclass(es) (say for ex. BobCat) cannot access the protected members (like secretAboutCat) from superclass (Cat) through an instance of superclass (For example, newCat.secretAboutCat) if they belong to different package than the super-class. Subclasses belonging to other packages can access protected members only through inheritance.

 

 

Protected members are accessible to all classes within its package

When you apply protected modifier to a member variable or method, it becomes available to all classes within the same package as the declaring class. For these classes, the protected modifier works exactly same as default modifier. Therefore all classes within the same package as Cat class can access the protected members. The subclasses of Cat class that are within the same package as Cat class (For example, the HouseCat class) inherit the protected members. In addition, they can also access protected members via reference. Listing 3.6 shows HouseCat class accessing the secretAboutCat variable via inheritance and also by creating a Cat object reference. Both ways are allowed.


 

Similarly the classes which are not subclasses of Cat, but are in same package as Cat (For example Chameleon) can also access protected variables via reference to a Cat class. They cannot directly access it as they do not inherit it. Listing 3.7 shows how the Chameleon can access the protected variable of Cat class.


Working with the protected modifier is simple if you keep inheritance in mind. Contrary to its name, it is much liberal modifier. In fact, it is more liberal than the default access. Therefore, classes which can access the default members of a class, can always access its protected members via reference.

 

Understanding accessibility with sample class hierarchy

Figure 3.3 explains the protected access modifier with the animal hierarchy. The protected members of public class Cat are accessible to the classes indicated by dark-gray color in the figure.

 


Figure 3.3 Classes in the same package as Cat and classes that extend[‡‡‡‡‡] Cat have access to protected members of Cat

 

As you can see in the figure, all the classes in package com.example.pets have access to the protected members of Cat. The inherited protected members of Cat are accessible to all the subclasses of Cat class irrespective of their package. Therefore, class BobCat can access the protected members of Cat class via inherited Cat instance. Note that the class Chameleon is not a subclass of Cat but it can access the protected members of Cat class as it is defined in the same package as Cat.