P
Philipp
Hello,
I just read about the "Call-Super" antipattern
(http://en.wikipedia.org/wiki/Call_super) and I'm not completely
convinced about the "anti-pattern" property of this construct.
Suppose you have a class hierarchy which makes sense, something like:
Car extends Vehicle
Ford extends Car
and they all have a start method:
Why is it bad for each start to call the super method? (in case each one
adds the super's behavior)
In particular, a proposed solution in the wiki article is to use the
template method pattern. How does that improve the design? In a
multi-inheritance structure as this example, you will still have to
choose between replicating code (Copy-Paste antipattern) or adding
template methods (hooks) for all possible subclasses.
Thanks for your comments.
Philipp
public class Vehicle {
public void start(){
// unlockDoor();
}
public static class Car extends Vehicle {
@Override
public void start() {
super.start();
// startEngine();
}
}
public static class Ford extends Car {
@Override
public void start() {
super.start();
//switchRadioOn();
}
}
}
I just read about the "Call-Super" antipattern
(http://en.wikipedia.org/wiki/Call_super) and I'm not completely
convinced about the "anti-pattern" property of this construct.
Suppose you have a class hierarchy which makes sense, something like:
Car extends Vehicle
Ford extends Car
and they all have a start method:
Why is it bad for each start to call the super method? (in case each one
adds the super's behavior)
In particular, a proposed solution in the wiki article is to use the
template method pattern. How does that improve the design? In a
multi-inheritance structure as this example, you will still have to
choose between replicating code (Copy-Paste antipattern) or adding
template methods (hooks) for all possible subclasses.
Thanks for your comments.
Philipp
public class Vehicle {
public void start(){
// unlockDoor();
}
public static class Car extends Vehicle {
@Override
public void start() {
super.start();
// startEngine();
}
}
public static class Ford extends Car {
@Override
public void start() {
super.start();
//switchRadioOn();
}
}
}