I have several classes that extend others. Both of them have a common result method. If I have an instance of foobarbaz , can I call its parent method / grandparent result ?
public class Foo { protected int resultA; public void calc(){ resultA=...} public void result(){ return resultA; } } public class Foobar extends Foo{ protected int resultA; public void calc(){ super.calc(); resultB=...; } public void result(){ return resultB; } } public class Foobarbaz extends Foobar{ protected int resultA; public void calc(){ super.calc(); resultC=...; } public void result(){ return resultC; } }
The problem I'm trying to solve is that each class does some extra computation besides one of its parents. If the user wants to get results from all three objects, CalculateManager knows that only Foobarbaz will need to be used and calculated. He then returns a link to Foobarbaz to the one who asks for Foo, because Foobarbaz will also have a result for Foo.
Sort of:
CalculationManager.add(Foo,Foobar,Foobarbaz); //The following 3 calls return the same reference to a Foobarbaz object Foo res1=CalculationManager.get(Foo); Foobar res2=CalculationManager.get(Foobar); Foobarbaz res3=CalculationManager.get(Foobarbaz); CalculationManager.doCalc(); //Iterate over each object to get result with the same method .result() res1.result(); //---> resultA res2.result(); //---> resultB res3.result(); //---> resultC
source share