If I have a class that can’t be changed (inside a jar),
Ex.
public class AA implements A{
private String s = "foo";
public String getValue() { return s; }
}
what would be a good way to override that getValue() method?
My way has been recopying the class. Ex.
public class AB implements A{
private String s = "foo";
public String getValue() { return s + "bar"; }
}
Thanks!
No matter what you do, you cant get access to the private variable (without reflection). If you needs its value, invoke the superclass’s getter in your getter, to get the value, then manipulate it as you will. You can invoke the superclass’s method by doing
super.getValue();inside your
getValueimplementation.Given your update
Note the following
1) Im using
extendswhich you do not.extendsis for extending a class,implementsis for implementing an interface.2) Im not shadowing
s. I’m leaving that in the super class. I just use the super’sgetValuein conjunction what the decoration you specified.