Let\'s say we have a concrete class Apple
. (Apple objects can be instantiated.)
Now, someone comes and derives an abstract class Peach
from Apple. It\'
Re Peach from Apple:
Re Tomato from Berry:
Juice()
- imposes certain requirements and makes certain promises. Derived classes' implementations of Juice()
must require no more and promise no less. Then a DerivedTomato IS-A Berry and code which only knows about Berry is safe.Possibly, you will meet the last requirement by documenting that DerivedTomatoes must call Berry::Juice()
. If so, consider using Template Method instead:
class Tomato : public Berry
{
public:
void Juice()
{
PrepareJuice();
Berry::Juice();
}
virtual void PrepareJuice() = 0;
};
Now there is an excellent chance that a Tomato IS-A Berry, contrary to botanical expectations. (The exception is if derived classes' implementations of PrepareJuice
impose extra preconditions beyond those imposed by Berry::Juice
).