Calling a parent class method on a java child class object

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 
+4
source share
4 answers

Design principle: composition preference over inheritance

Get a copy of Head First Design Patterns and read about Strategy, Template, and possibly Factory as well. Other models discussed there will certainly come in handy elsewhere in this project or in future projects.

Possible scenario:

  • the Foo, FooBar, and FooBarBaz classes extend from the AbstractFoo class
  • provide an AbstractFoo attribute of type Calculator myCalc (and getter and setter for this)
  • make An interface calculator with the doCalc () method, which will be implemented by classes that implement this interface
  • create all possible implementations of the calculator (CalculatorX, CalculatorY, ..) that implement their own version of doCalc ()
  • provide AbstractFoo with a public int calc () method that calls the myCalc.doCalc () method to get the desired results.

At that moment when you are worried that the various implementations of the calculator have a common code, you can use the template template or extend it from BaseCalculator, etc.

This should keep you energized for some time, but if you do it right and get it, you will find that using such patterns helps you in many programming situations. Consequently, name patterns, I think; -)

+1
source

you will need something similar in each class:

 Foo: printParent() { super.printParent(); } FooBar: printParent() { super.printParent(); } FooBarBaz: printParent() { print(); // this is the top most class //Incredibly fragile! } 
+2
source

This is not real. You will need to instantiate the object of the parent class in order to invoke its print method. A.

eg.

  FooBar f = new FooBar(); f.print(); //foobar 

Only from within the subclass can you use the super keyword to access the superclass method.

+1
source

Super calls the parent class method

0
source

All Articles