Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call a protected method from an inheriting class in another package in Java?

Say there's the following base class:

package bg.svetlin.ui.controls;

public abstract class Control {
    protected int getHeight() {
        //..
    }
    //...
}

Also, in the same package, there's a class that inherits:

package bg.svetlin.ui.controls;

public abstract class LayoutControl extends Control {
    public abstract void addControl(Control control);
    //...
}

Then, there's a third class in another package:

package bg.svetlin.ui.controls.screen;

public abstract class Screen extends LayoutControl {
    //...
}

And, finally, there's the implementation class, again in a different package:

package bg.svetlin.ui.controls.screen.list;    

public class List extends Screen {

    private final Vector controls = new Vector();

    public void addControl(Control control) {
        height += control.getHeight();
        controls.addElement(control);
    }
}

Even though List inherits from Control, and the getHeight() is protected, there's the following error:

getHeight() has protected access in bg.svetlin.ui.controls.Control

I've checked that my imports are right. I'm using NetBeans.

Any idea what's wrong? I thought protected fields and methods are visible to the children even if the latter are in a different package.

Thanks!

like image 631
Albus Dumbledore Avatar asked Mar 03 '12 12:03

Albus Dumbledore


People also ask

How can I call a protected method from another package?

The protected access modifier is accessible within the package. However, it can also accessible outside the package but through inheritance only. We can't assign protected to outer class and interface. If you make any constructor protected, you cannot create the instance of that class from outside the package.

Can protected methods be accessed by classes in different package?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

Is it possible to call a protected data members of one package in another package?

We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.


1 Answers

I thought protected fields and methods are visible to the children even if the latter are in a different package.

That's correct. The class itself has an access to the inherited protected members. But, what you're trying to do it to call the getHeight method on some Control reference. You're allowed to call it only on this instance!

For a better understanding, let me quote Kathy Sierra's SCJP Preparation Guide:

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. The subclass can see the protected member only through inheritance.

like image 59
jFrenetic Avatar answered Sep 27 '22 20:09

jFrenetic