Protected variables can be accessed in a child class or child

Whether a variable is available protectedin parentObjectof any child Object? or can they be accessed only with help childObject? I have a script that clearly expresses my doubts.

I have two classes ParentClassand ChildClass. ParentClassis the parent element ChildClass. I have a variable protectedin ParentClasswith a name protVar. He has a type Object. Then I create two Objectas follows.

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 can I access protVarfrom p1from c1?

+4
source share
4

: . . , , .

protected Java. , "" , .

, Child Parent, Parent . , Child, - Child, Parent. , ?

Core Java 9th Edition:

Manager leaseDay , Employee. ,

( Manager Employee, Employee , )

,

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!
    }
}

, ( this, , )

, OP: callMethod ChildClass, , , . , callMethod ParentClass, , ParentClass ParentClass.


Update:

, , JLS, , :

( http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2.1 , )

E.Id E.Id(...) E:: Id, E - (§15.8), , E S S

, , , :

Manager manager.hireDay , Manager , , Manager Manager Manager.

, JLS, manager.hireDay DOES, Manager ( ).

+7

, "" .

, , . .

+1

, [ ], .
1 - getter
2 - ,

public void callMethod(ParentClass o) {
    try {
        Field f = o.getClass().getDeclaredField("protVar");
        f.setAccessible(true);
        Object value = f.get(o);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
0

, , , java.

Employee p1.

package p1;
public class Employee{
    protected String hireDay = "hireday";
}

Employee .

package p2;
public class Manager extends p1.Employee {
 //now even Manager class has hireDay protected variable that was defined in Employee class.
 //Since Manager class has protected variable hireDay , for any manager m1 , we can access hireDay as m1.hireDay ONLY within package p2.
}

enterpriseentur p3.

package p3;
public class Enterpreneur extends p2.Manager{

    //now  Enterpreneur class has inherited hireDay protected variable that  Employee class had. (see comments in Employee class.)
 //Since Enterpreneur class has protected variable hireDay , for any Enterpreneur e , we can access hireDay as e.hireDay ONLY within package p3.



    //the following will work because using Enterpreneur reference e , we can access e.hireday within package p3 , this has nothing to do with
    //the fact that right now our code is present in Enterpreneur class , like the other answer said. Note the method is static.
    public static void printhireDay(Enterpreneur e){
        System.out.println("hireday is :" + e.hireDay);
    }

    //this will work because using this reference we can only access protected variable in the same class(Enterpreneur) or in a subclass.
    public void printhireDay(){
        System.out.println("hireday is :" + this.hireDay);
    }

    // This shouldn't work because using manager reference , we can only access protected field within package p2.
    /* public printhireDay(Manager m){
        System.out.println("hireday is :" + m.hireDay)
    }*/
}

.

public class HelloWorld{

     public static void main(String []args){
        p3.Enterpreneur e = new p3.Enterpreneur();
        //both of these work.
        e.printhireDay();
        //printing by passing the reference e.
        p3.Enterpreneur.printhireDay(e);


     }
}
-1
source

Source: https://habr.com/ru/post/1529737/


All Articles