tags:

views:

1497

answers:

5

Hi there,

I'm new to interfaces and abstract classes. I want to create a couple of interfaces to define core methods and variables for the objects for a shopping cart system. Then I want to create abstract classes which implement the core functions. The idea is that they can be used by other classes in slightly different ways for different projects.

Here are my (cut-down) interfaces:

public interface ICart
{
    ...
    List<ICartItem> CartItems { get; set; }
}

public interface ICartItem
{
    int ProductId { get; set; }
    int Quantity { get; set; }
}

And here is my abstract Cart class (again, only showing relevant lines) which implements ICart:

public abstract class Cart : ICart
{

    private List<CartItem> _cartItems = new List<CartItem>();

    public List<CartItem> CartItems 
    {
        get
        {
            return _cartItems;
        }
    }
}

And my CartItem class which implements ICartItem:

public abstract class CartItem : ICartItem
{
    ...
}

When I try to compile the classes I get an error saying: 'Cart' does not implement interface member 'CartItems'. 'Cart.CartItems' cannot implement 'ICart.CartItems' because it does not have the matching return type of System.Collections.Generic.List<ICartItem>.

I thought that the idea here is that the interface can be implemented by a number of classes which perform the core functions in different ways and add new methods, etc. Why would my interface need to know what class is actually being used, just as long as that class actually implements the interface correctly?

Any help appreciated!

Thanks,

David

+8  A: 

You need to user generics in C# 2 to achieve that:

public interface ICart<T> where T : ICartItem
{
    // ...
    List<T> CartItems { get; set; }
}

public abstract class Cart : ICart<CartItem>
{
    // ...
    public List<CartItem> CartItems { get; set; }
}
DrJokepu
generics are available as from c# 2.0 :)
Frederik Gheysels
This is a pretty good way to implement this!
mquander
(fixed the c# 2 thing; hope you don't mind...)
Marc Gravell
@Marc Gravell: Yeah what I meant by C# 3 that it does not offer covariance yet. Funny thing is that I had exactly you in mind when I decided not to mention that issue because I was afraid that you will pop up out of the blue and school me about C# 4 covariance only applying to read-only collections
DrJokepu
A: 

ICart specifies a getter and setter for CartItems but your implementation only has a get.

Jamie Ide
+3  A: 

In your abstract class, you should define that the return type of the CartItems property, is of type List<ICartItem>.

But, if you really want that the property is of type List<CartItem>, you can maybe do this:

public interface ICart<TCartItem> where TCartItem : ICartItem
{
   List<TCartItem> CartItems { get; set; }
}

public interface ICartItem
{
}

public abstract class Cart : ICart<CartItem>
{
     public List<CartItem> { get; set; }
}

public abstract class CartItem : ICartItem
{
}
Frederik Gheysels
+2  A: 
Reed Copsey
+2  A: 

An interface is an agreement. A contract, if you will, between you and anyone who uses your class. By telling folks that you are implementing the ICart interface, you are promising them that all the methods in the interface exist on your class. Thus, all your methods must match the signatures of the interface methods.

In order to return of list of items that implement ICartItem, you need to use generics as suggested by DrJokepu. This tells everyone that the interface only gurentees a list of objects that implement ICartItem, not ICartItem specifically.

public interface ICart<T> where T : ICartItem
{
    List<T> CartItems { get; set; }
}
Joel Potter