Tim Hibbard

CEO for EnGraph software
posts - 626, comments - 1586, trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

OO Question

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();
        }
    }

Print | posted on Wednesday, May 02, 2007 11:24 AM | Filed Under [ .NET ]

Feedback

Gravatar

# re: OO Question

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...

5/2/2007 12:34 PM | Brian Biales
Gravatar

# re: OO Question

//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();
}
5/2/2007 12:39 PM | Brian Biales
Gravatar

# re: OO Question

ahhhh...very nice!!

Thanks Brian! That is exactly what I was looking for.
5/2/2007 1:15 PM | Tim Hibbard
Gravatar

# re: OO Question

I was just going to recommend you to only declare (but not define) the methods "abstract", but Brian beat me to it :-)
5/2/2007 3:16 PM | Patrik
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: