When would I use an Interface vs Extending a class

I need some help understanding when to use an Interface and when to just extend a class.  It is my understanding that using an Interface allows me to iterate over different classes that implement the interface without having any unexpected results.

For example:
class Customer
{
    public string someProperty { get; set; }
    public virtual void Add()
    {
        // code here - make a call to private method in class
    }

    public virtual void Delete()
    {
        // code here
   }

   public Update()
   {
        // code here
   }

   private void PrivateMethodInClass()
   {
       // code here
   }
}

class AllowDelete : Customer
{
    // Can use the Add and Delete methods defined in Customer
}

class NoDelete : Customer
{
    public override void Delete()
    {
        throw new Exception("Not allowed");
    }
}

Open in new window

In the example I can't iterate over a collection that contains both a DeleteAllowed Customer and a  NoDelete Customer if I call the Delete method because the NoDelete Customer will throw an error.

In that case I would define interfaces for the Add method and Delete method as follows:
interface IAdd
{
    void Add();
}

interface IDelete
{
    void Delete();
}

class Customer : IAdd, IDelete
{
    public virtual void Add()
    {
        // add code
    }

    public virtual void Delete()
   {
        // add code
   }
}

class AllowDelete : IAdd, IDelete
{
    // Question:  Can AllowDelete use the Add and Delete method in Customer? 
}

class NoDelete : IAdd
{
    // Question: Can NoDelete use the Add method in Customer?
}

Open in new window


The above are just simple classes to illustrate where my questions come from and don't represent real classes in any way.

Here are my questions:

1.  Base Class Customer has 3 public methods:  Add, Update, Delete.  All Customers types can Add and Update but not all can Delete.  With the way I have the Customer class and Interfaces defined, is the Update Class available to  the AllowDelete Class and NoDelete Class?  (i.e. no interface has been defined for Update)

2. Is the someProperty property available to the the AllowDelete and NoDelete classes?

3.  Should the interfaces be defined in a separate file that can then be used as a reference in other classes that may need to create a AllowDelete or NoDelete Customer?

I just don't understand how the Interface knows about the other methods and properties of a class (if it does).  If the Interface doesn't know about the other methods and properties does that mean I have to recreate all the methods and properties of the base class?  That doesn't seem right.

So besides my questions above, what would I do if I wanted to create a new Customer that has the same properties and methods of the Customer class but I want to add a new property to the Customer?  I don't want to add the new property to the base Customer class because I wouldn't want to change the base class once it has been deployed to a production environment.

I appreciate any help in clearing up my confusion.
dyaroshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
1) In the first example, all methods are available to all classes (although NoDelete will throw on Delete).   In the second example, AllowDelete and NoDelete are NOT subclasses of Customer, and thus have access only to those methods defined in the interfaces they support....not only that, they have to actually IMPLEMENT those methods themselves, they don't get the corresponding methods from the Customer class.

2) In the first example, yes.  In the second example, no.

3) This is generally considered best practice, although not actually required for Just Making Things Work.

Where Interfaces really show their strength is not in just defining classes....it is when situations arise where somebody has an interface (say IWidget) and some other class has a collection of IWidget (as opposed to a class that has a collection of Widget objects).

Somebody that wants to put an object in this collection can now do this by just implementing the needed IWidget properties and methods, even though this object might be otherwise of a completely unrelated class (as opposed to being able to only place objects developed with the class Widget in mind in the first place).
0
dyaroshAuthor Commented:
Thanks for the clarification.  I think I understand a little better now.  So here is another question.

The Customer class implements the IAdd and IDelete interfaces.  If I need the same functionality that is in the Customer class and want to extend the Customer class to add additional properties and methods would the new Extended class still be of type IAdd and IDelete so that I could add a Customer and NewCustomer to the same list and be able to iterate over them?  Here is the definition:

interface IAdd
{
    void Add();
}

interface IDelete
{
    void Delete();
}

partial class Customer : IAdd, IDelete
{
    public string someProperty { get; set; }
    public virtual void Add()
    {
        // code here - make a call to private method in class
    }

    public virtual void Delete()
    {
        // code here
   }

   public Update()
   {
        // code here
   }

   private void PrivateMethodInClass()
   {
       // code here
   }
}

partial class NewCustomer : Customer
{
    public string newpropertyfornewcustomer { get; set; }

   public void newmethodfornewcustomer()
   {
        // Code here
   }
}

Open in new window


Here are the questions I have with the above example:

1.  NewCustomer would be able to call the Add, Update, and Delete methods from the Customer class.  Correct?
2.  Can a Customer and NewCustomer be added to a List of type IAdd and have the Add method called?

I appreciate your help.  Your explanations are really clearing things up for me!
0
Jens FiedererTest Developer/ValidatorCommented:
1)  Correct.  
2)  Correct.

Aside from one syntax error (Update needs a return type) your example is spot-on.

NewCustomer gets the IAdd interface and the Add method for free (through inheritance).

If you added another class NewVendor with NO base class to support the IAdd interface, you would have to implement that Add method in NewVendor yourself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
Thank you so much.  You cleared things up for me very nicely!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.