interface ICanvasTool
{
void Motion(Point newLocation);
void Tick();
}
abstract class CanvasTool_BaseDraw : ICanvasTool
{
protected abstract void PaintAt(Point location);
public override void Motion(Point newLocation)
{
// implementation
}
}
class CanvasTool_Spray : CanvasTool_BaseDraw
{
protected abstract void PaintAt(Point location)
{
// implementation
}
public override void Tick()
{
// implementation
}
}
This doesn't compile. I could add an abstract method "Tick_Implementation" to CanvasTool_BaseDraw, then implement ICanvasTool.Tick in CanvasTool_BaseDraw with a one-liner that just calls Tick_Implementation. Is this the recommended workaround?
The way to do this is to add an abstract void Tick() method to CanvasTool_BaseDraw and override it in CanvasTool_Spray.
Not every programming language does it this way. In Java you do not have to add an abstract method for every method in the interface(s) you implement. In that case your code would compile.
You have a few things mixed up..
Motion should be virtual in your base class so that it may be overridden in child classes. Your child class needs to make PaintAt override instead of abstract. The base class needs to implement Tick as an abstract method.
interface ICanvasTool
{
void Motion(Point newLocation);
void Tick();
}
abstract class CanvasTool_BaseDraw : ICanvasTool
{
protected abstract void PaintAt(Point location);
public virtual void Motion(Point newLocation)
{
// implementation
}
public abstract void Tick();
}
class CanvasTool_Spray : CanvasTool_BaseDraw
{
protected override void PaintAt(Point location)
{
// implementation
}
public override void Tick()
{
// implementation
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With