Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected variables can be accessed within Child Class or Child Object

Is the protected variable of a parentObject be accessed from any child Object? or can be accessed only by only the particular childObject? I have a scenario which clearly expresses my doubt.

I have two classes ParentClass and ChildClass. ParentClass is parent of ChildClass. I have a protected variable in ParentClass named protVar. It is of type Object. Then, I create two Objects like the following.

ParentClass p1 = new ParentClass();
ChildClass c1 = new ChildClass();
c1.callMethod(p1); // Here I want to access protected variable of p1 which is a separate object and Not initialized within c1 as super()

Now will I be able to access the protVar of p1 from c1?

like image 602
Santron Manibharathi Avatar asked Mar 03 '14 05:03

Santron Manibharathi


2 Answers

Disclaimer: the answer is copied from my answer of another question. However no answer from that question is accepted. I believe it also suit this question therefore I am copying the content with some minor editing to here.

protected is a bit interesting in Java. Although we always says "protected" give access to subclass of different package, it is not the whole picture.

For example, if you have Child extending Parent, and there is a protected member in Parent. What you can do in Child is to access that protected member of Child, but not even that protected member of Parent. Sounds a bit strange right although they sounds the same thing?

Quoted from Core Java 9th Edition:

However, the Manager class methods can peek inside the hireDay field of Manager objects only, not of other Employee objects. This restriction is made so that you can’t abuse the protected mechanism by forming subclasses just to gain access to the protected fields

(class Manager extends Employee, and there is a hireDay protected member in Employee, and Manager and Employee are located in DIFFERENT package)

For example,

public class Manager extends Employee {
    // accessing protected member of itself
    public void foo1() {   
        System.out.println("" + this.hireDay);  // OK
    }

    // access protected member of instance of same type
    public void foo2(Manager manager) {  
        System.out.println("" + manager.hireDay);  // OK
    }

    // access protected member of instance of super-class
    public void foo3(Employee employee) {
        System.out.println("" + employee.hireDay);  // NOT ALLOWED!
    }
}

Which means, protected member allow child class from another package to access through reference of that child class (either this, or another reference that is-a child class)

And, be specific to OP's answer: if callMethod is declared in ChildClass, then NO, you cannot do it and it will not even compile. However if callMethod is declared in ParentClass then everything is fine, because it is simply ParentClass accessing protected member of a ParentClass instance.


Update:

Given criticisms in comment, I think it worth to go to JLS to see what it say:

(Quoted from http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2.1 about Access to a protected Member, second bullet)

If the access is by a field access expression E.Id, or a method invocation expression E.Id(...), or a method reference expression E :: Id, where E is a Primary expression (§15.8), then the access is permitted if and only if the type of E is S or a subclass of S

This is essentially what I was trying to deliver in the answer:

Within Manager class, manager.hireDay works because manager is a primary expression, and the access is permitted because type of manager is Manager or subclass of Manager.

So, based on JLS, why manager.hireDay works DOES have relationship with type of manager (being the same type).

like image 111
Adrian Shum Avatar answered Dec 22 '22 17:12

Adrian Shum


Yes, a derived class can access a protected variable in a base class via both 'super' and another reference to the base class.

EDIT It should be noted that I am here assuming the same package as you didn't state anything about different packages. The rules are different otherwise.

like image 27
user207421 Avatar answered Dec 22 '22 18:12

user207421