[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

When would I use an Interface vs Extending a class

Posted on 2014-08-02
4
Medium Priority
?
134 Views
Last Modified: 2014-08-03
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.
0
Comment
Question by:dyarosh
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40236878
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
 

Author Comment

by:dyarosh
ID: 40237335
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
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 2000 total points
ID: 40237434
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
 

Author Closing Comment

by:dyarosh
ID: 40237575
Thank you so much.  You cleared things up for me very nicely!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question