Answers for "how to call private method from public method in java"

0

java private method can't call public method

class X{
    private int field = 1;
    private void method(){}
    void foo(X x){
        x.field = 2;
        x.method(); // this is OK, because we are accessing members from instance of X 
                    // via reference of class X (which is same class as this one)
    }

    void bar(Y y){// = lets assume that Y extends X
        y.field = 3;
        y.method(); // ERROR: we can't access `method()` 
    }
}
Posted by: Guest on May-14-2021

Code answers related to "how to call private method from public method in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language