Madmenyo
Madmenyo

Reputation: 8584

Access to protected field in field of same parent?

Why is it that I have access to and visibility of protective fields inside classes that share the same parent? I always figured that protected could only be accessed through the parent or the child itself not outside in any way.

class Parent {

    protected int age;
}

class Sister extends Parent { }

class Brother extends Parent {

    public void myMethod(Sister sister) {
        //I can access the field of my sister,
        // even when it is protected.
        sister.age = 18;
        // Every protected and public field of sister is visible
        // I want to reduce visibility, since most protected fields
        // also have public getters and some setters which creates
        // too much visibility.
    }
}

So I guess it's only protected from outside of the family. Why is this and how can I have something hidden even from family members other then the direct parent and the child? To me it seems we're lacking an access member modifier. Something like family should actually be protected and protected should be hidden from all but the child and parent. I'm not asking anyone to rewrite Java, just noticing.

Upvotes: 1

Views: 1476

Answers (4)

MC Emperor
MC Emperor

Reputation: 22997

That is because the classes Parent, Brother and Sister are in the same package. Members within the same package are always visible, except with the private modifier.

This code:

public class Sister {

    void someMethod() {
        Brother brother = new Brother();
        brother.age = 18;
    }
}

means you are working in the Sister class, and from there, you're trying to access the age member of the Brother class. Brother has nothing to do with Sister, except the fact that they accidentally extend the same parent class.

The only reason accessing the age member is valid, is because Brother and Sister are within the same package. Try to move either Brother or Sister to another package, and you'll see that the compiler starts complaining.

Upvotes: 3

user140547
user140547

Reputation: 8200

If Brother and Sister are not in the same package as Parent, then the protected variable age of other instances is not visible. see Why can't my subclass access a protected variable of its superclass, when it's in a different package?

example:

package test.family.parent;

public class Parent {
    protected int age;
}


package test.family.child;
import test.family.parent.Parent;

public class Brother extends Parent {

public void test(){
        this.age = 1;
        Brother brother = new Brother();
        brother.age = 44; // OK
        Sister sister = new Sister();
        sister.age = 44;  // does not compile
    }
}

package test.family.child;
import test.family.parent.Parent;

public class Sister extends Parent {

    public void test(){
        this.age = 1;
        Brother brother = new Brother();
        brother.age = 44;  // does not compile
        Sister sister = new Sister();
        sister.age = 44;   // OK
    }
}

In this example, Sister can access age of itself and other instances, but not those of Brother

Upvotes: 2

Bonatti
Bonatti

Reputation: 2781

From the documentation you can see the following behaviors: public, protected, and private

public means everyone can view/alter it.

protected means that it its package and subclasses can view/alter it

private means its for the class only to view/alter it.

Also, check here for examples and easy to understand descriptions

Edit:

As a rule of thumb, think that a class extends another when "oneClass" is an "anotherClass", what you have written means that "brother" is a "parent", while it should be something as "brother" is a "person" and "sister" is a "person".

Right now, both are brother/sister and parent at the same time, leading to some confusion on what you are trying to perform

Edit 2:

class parent{
   private String age;
}

Upvotes: 2

Florian Schaetz
Florian Schaetz

Reputation: 10652

Modifiers are for classes. Your Brother is a parent, too, so it can access parent.age, since that field is protected. It doesn't matter if the actual object in question is this brother, another brother, a sister or any other parent.

Upvotes: 0

Related Questions