I'm new to java and getting the hang of protected visibility.
I have Class A which has protected method method1 as follows -
public abstract class A {
public A(...){}
protected void method1(){
// do something
}
Now I have another Class B which extends a different Class C. Class B has a nested static class, Class D extending Class A
public class B extends C {
public B(...){
super(...);
}
private static class D extends A {
D(...){super(...);}
}
public void method2() {
D record = new D();
record.method1(); // need to access the protected method1 of class A, which is the superclass of nested class D. Getting the error here
}
}
I get this error - method1 has protected access in Class A Now my question is how can I access method1? I cannot change it to public in class A

D.method1();, you need to userecord.method1(). I guess this is what you wanted to do anyways becauserecordis not used otherwise.record.method1(). The method isn't static, so you call it on an instance of Arecord- an immutable class for tuples, likerecord Complex(double re, double im) {}; Complex z = new Complex(3.0, 4.0); double r = Math.hypot(z.re(), z.im());