Let's say I have a base class that requires inheriting classes to implement a specific interface. Is there a keyword or attribute I can put on my base class to tell inheriting classes that they must implement a specific interface? Or do I have to implement the interface in my base class, then also implement the interface in inheriting classes and override the base implementation?
Basically, I want to know if there is a cleaner way of accomplishing this
public interface ICar
{
void UnlockCar();
void StartCar();
void Move();
}
public abstract class BaseCar : ICar
{
public void DriveAway()
{
//Will call the methods from
//the inheriting class
UnlockCar();
StartCar();
Move();
}
public virtual void UnlockCar() {}
public virtual void StartCar(){}
public virtual void Move() {}
}
public class OldCar : BaseCar, ICar
{
public override void UnlockCar()
{
Key.PutInDoor();
Key.Turn();
}
public override void StartCar()
{
Key.PutInIgnition();
Key.Turn();
}
public override void Move()
{
Clutch.Press();
GearShaft.PutInFirst();
Clutch.ReleaseSlowlyWhilePressingGas();
}
}
public class NewCar : BaseCar, ICar
{
public override void UnlockCar()
{
Remote.PressUnlock();
}
public override void StartCar()
{
Key.PutInIgnition();
Key.Turn();
}
public override void Move()
{
Brake.Press();
GearShaft.PutInDrive();
Brake.Release();
Gas.Press();
}
}
public class Consumer
{
public void MoveCars()
{
new OldCar().DriveAway();
new NewCar().DriveAway();
}
}