Wednesday, September 5, 2012
Today I am going to post how the access-specifier protected is related with inheritance in Java. It is less restricted than default but restricted than public.
How protected works and is different from default:Default and protected behavior differ only when we talk about subclasses.If the
protected keyword is used to define a member,any subclass of the class declaring the member can access it through inheritance.It doesn't matter if the superclass and subclass are in different packages,the protected superclass member is still visible to the subclass in a very specific way.This is in contrast to the default behavior,which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass.
 Whereas default access doesn't extend any special consideration to subclasses (you're either in the package or you're not), the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think package restriction. No exceptions. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package. But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.

Showing accessibility of protected
The code below will explain it further

package package1;
public class Parent {
protected int x = 20;
// protected access

package package2;
import package1.Parent;
class Child extends Parent {
   public void testIt() {
      System.out.println("x is " + x);
// No problem;Child inherits x
      Parent p = new Parent();
// Can we access x using p reference?
      System.out.println("X in parent is " + p.x);
// Compiler error!

The last line will give an error as following
package2/ x has protected access in
System.out.println("X in parent is " + p.x);
1 error

Hope this helps. Keep looking for more posts.


  1. To my mind that's what I call a good blog article! Do you utilize this website for private goals solely or you have it to get profit with its help?

    1. I am just using this blog to help other people nad encourage them about Java


Total Pageviews

Subscribe via Email


Popular Posts