Code:
ClassA extends ClassBase {
protected IntefaceA parA;
.... }
InterfaceA extends InterfaceBase
ClassBase {
protected IntefaceBase parB;
public method1() {
parB.someMethod();
}
Code:
ClassA testClass=new ClassA();
testClass.setParA(new InterfaceA {....}; );
testClass.method1();
I receive null pointer exception because method metho1 uses class variable from ClassBase that used by method1 is parB. Does it possible have method1 use parA?
If not, I need copy&paste all code of method1 from base class to descendant.
Thanks.
Overriding fields is not possible in Java, only overriding methods. So what you can do is hide your
parA/parBmembers behind aprotected virtualgetter, and access them only via the getter method. Then override the getter inClassAto returnparAinstead ofparB: