Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

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();
        }
    }
Posted on Wednesday, May 2, 2007 11:24 AM .NET | Back to top


Comments on this post: OO Question

# re: OO Question
Requesting Gravatar...
With the concept of an abstract class, you don't even need the interface at all. Because BaseCar implements ICar (even abstractly), I believe your line:
public class NewCar : BaseCar, ICar

is really the same as:
public class NewCar : BaseCar

But you need to make one change... You need to NOT define those three methods (get rid of the braces {}) in BaseCar. By declaring these methods but not defining them (which you can only do in an abstract class) this implies that they MUST be defined (implemented) in any non-abstract inheritor...

Left by Brian Biales on May 02, 2007 12:34 PM

# re: OO Question
Requesting Gravatar...
//Try this...
public abstract class BaseCar
{
public void DriveAway()
{
//Will call the methods from
//the inheriting class
UnlockCar();
StartCar();
Move();
}
public abstract void UnlockCar();
public abstract void StartCar();
public abstract void Move();
}
Left by Brian Biales on May 02, 2007 12:39 PM

# re: OO Question
Requesting Gravatar...
ahhhh...very nice!!

Thanks Brian! That is exactly what I was looking for.
Left by Tim Hibbard on May 02, 2007 1:15 PM

# re: OO Question
Requesting Gravatar...
I was just going to recommend you to only declare (but not define) the methods "abstract", but Brian beat me to it :-)
Left by Patrik on May 02, 2007 3:16 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net