I have a java public class I need to modify (only one method). The class is in a package so I’m writing a new class that extends the first class and overriding the method I need to change.
Class A is
public class A {
GL gl;
GLU glu;
PGraphicsOpenGL pgrap;
//other fields
//constructor
public void method() {
this.gl = pgrap.gl;
this.glu = pgrap.glu;
//something else I don't want in class B
}
}
Class B is something like
public class B extends A {
//constructor that recalls super()
public void method() {
super.gl = pgrap.gl;
super.glu = pgrap.glu;
}
}
but I get an error for super.gl = pgrap.gl: The field A.gl is not visible.
I don’t have any getter method written in the package, what should I do?
Thanks.
NOTE: I am not able to recompile the package or add the class B to the package.
The default access specifier is
package-privatewhich means classes in the same package asAcan access this variable using the instances ofAAnd
package-privatemembers (andprivatemembers) are not inherited, onlyprotectedandpublicmembers are.Since
A#method()is already doing the assignment operation, you callsuper.method()inB#method()to get your desired behavior. Or you should mark them asprotected.