java private method can 27t call public method

Solutions on MaxInterview for java private method can 27t call public method by the best coders in the world

showing results for - "java private method can 27t call public method"
Lea
25 Sep 2020
1class X{
2    private int field = 1;
3    private void method(){}
4    void foo(X x){
5        x.field = 2;
6        x.method(); // this is OK, because we are accessing members from instance of X 
7                    // via reference of class X (which is same class as this one)
8    }
9
10    void bar(Y y){// = lets assume that Y extends X
11        y.field = 3;
12        y.method(); // ERROR: we can't access `method()` 
13    }
14}
15