public class Parent{
private Object oBase;
public Object getObject(){
// [some logic]
return oBase;
}
public String getObjectValue(){
return getObject().getValue();
}
public class Child extends Parent {
private Object oChild;
public Object getObject(){
return oChild;
}
public Object getObjectValue(){
return getObject().getValue();
}
public String getParentObjectValue(){
return super.getObjectValue();
}
}
In the above template, I need a way to make sure that the getObject() in Parent.getObjectValue() calls Parent.getObject() and not Child.getObject(), so that the child class can have getters to oBase and oChild.
I've tried using ((Parent)this).getObject().getValue() in Parent.getObjectValue(), but it still polymorphs to the child definition. Is there way to force static binding in java for a specific call?
I'm trying to avoid duplicating the [some logic] from Parent.getObject() in Parent.getObjectValue(), but I guess I'll have to if there's really no way around it.
You can either make the getObject()
method private, or change the method name so that polymorphism will not kick on. Overall, you'll have to rethink your design.
Other options are to extract the [some logic] to a third method and invoke this from both getObject()
and getObjectValue()
. Please keep in mind the Command Query separation principle when you use this design.