Question

I was reading about protected modifier in Java which fields can be accessible inside same package and subclass.

Now I have written some code.

package com;

public class Parent {

    protected void print()
    {
        System.out.println("dFDF");
    }
}

Now subclass.

package abstraact.concept;

import com.Parent;

    public class BaseParent extends Parent{

        public void printNum()
        {
            Parent p = new Parent();
            p.print(); /** Getting error here */
                    // The method print() from the type Parent is not visible
        }



        public static void main(String[] args) {
            BaseParent pp = new BaseParent();
            pp.printNum();

        }
    }

Why I am getting error? Since protected method/variables are accessible from subclass.

Était-ce utile?

La solution

This is a common "paradox" in Java, which is really no paradox at all. Simply put, one cannot access a protected method via an object reference in another package, due to the security (visibility) rules of the language.

Once a new object is declared, the visibility it has (or allows) is governed by the package where the code belongs, not by the inheritance hierarchy.

Calling

super.print();

or

this.print();

will work.

Autres conseils

Try and understand the statement carefully. Protected is visible only in the derived class. This is true for inheritance. But when you create an Object of type Parent you are not using inheritance. This means that the Parent is not derived there and the function of parent is protected. However if you use super.print() it will refer to Parent through inheritance and get that function.

So your solution is to replace:

Parent p = new Parent();
p.print();

with

super.print();

Call super.print() instead of declaring a new Parent Object

You cant access the protected method if the package is different.You can access this method as below :

 BaseParent pp = new BaseParent();
 pp.print();

or

super.print();

Declaring the print method as protected allows it to be inherited by the subclass BaseParent.

When code within BaseParent references its own (inherited) version of "print", this is allowed because it has become part of that subclass. However, the subclass still cannot reference the parent class's own version of the method (though being in the same package would overrule this) because the "protected" keyword only allows subclasses in other packages to inherit and use their own versions of the method.

After struggling for a while to grasp why the error turns up, that is how I have come to think about it.

Oracle's documentation in section 6.6.2 http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2.1 states this in their own way:

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object

thus BaseParent is responsible only for its own inherited print methoid, and not the original in the parent class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top