?
Solved

Inherit interface from a generic list

Posted on 2009-04-17
7
Medium Priority
?
527 Views
Last Modified: 2012-05-06
Hello

How can I inherit a an iterface from generic list, like below -

public interface IEmployeeCollection : List<IEmployee>
{
}

IEmployee is again an interface.

Thanks
0
Comment
Question by:praveenuni
  • 5
  • 2
7 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24167525
List is not an interface. The interface of List is IList. You can inherit from IList<T> like this:



public interface IEmployeeCollection<T> : IList<T>
{
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24167555
That IEmployee is an interface is not important. If you want to create a concrete class of an IEmployeeCollection, you first need to build this concrete class, which is still generic. Then, you can instantiate a new instance of this concrete class like so:


// instantiating a concrete list:
IEmployeeCollection<IEmployee> myEmplCollection = new ConcreteEmployeeCollection<IEmployee>();
 
//

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24167596
Since you are talking of generic programming, and you seem to want to create a non-generic substrate of IList (you call it specifically IEmployeeCollection, the name implies that it is NOT generic), it might be that building a new generic collection interface is not the right design choice here. May be all you are after is actually:

  List<IEmployee> emplList = new List<IEmployee>();

which is a list containing the type IEmployee.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:praveenuni
ID: 24167772
Thank you for the replies.

What i want to do here is this -

I have 2 classes which should contain EmployeeCollection. But on both the classes I don't want to say
EmployeeCollection : List<IEmployee>. I want to do this on interface level, so all the class has to say is
EmployeeCollection : IEmployeeCollection.

I tried the first method you suggested -
public interface IEmployeeCollection<T> : IList<T>
{
}

But if I'm inheriting from IList then I have to implement all the methods, which I dont want to do. I'm looking for option where I can say like EmployeeCollection : List<IEmployee> (similar to the way we declare in classes) and forget about implementation
0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24168415
I seems to me that you are mixing two OO concepts. At one point you are saying you want the IEmployeeCollection to be an interface and that any EmployeeCollection must inherit from that, but later you say that you do not want to implement all methods defined in the interface.

Unfortunately (but that is meant to be like that) in OO you use an interface to have some kind of skeleton without any implementation and you can inherit from that. In fact, you can inherit from many such skeletons. But! You need to implement every single method of that skeleton interface.

If you want to inherit and you do not want to copy every single method, which is understandable, you should not try to create an interface, but a class instead. Classes inheriting from your class can specify which methods to override and which methods to keep.

Now, for the moment I will assume you know all this already and that you want to have the best of both worlds: an interface, based on IList (I assume you want to expand that interface, otherwise it is totally useless) and an implementation. The answer is simple: tell the people that are going to use your interface should also inherit from the class List, so that they do not need to rebuild all the methods themselves.

Something like this:

public interface IEmployeeCollection<T> : IList<T>
{
    void someSpecialMethod();
}
 
public class ConcreteEmplCollection<T> : 
    List<T>,               // inherit from class List to make things easy
    IEmployeeCollection<T> // and inherit from your own interface
{
   //now people only have to implement someSpecialMethod:
   void someSpecialMethod() {
      // do something special
   }
}

Open in new window

0
 

Author Comment

by:praveenuni
ID: 24169726
Thank you for the detailed solution. It was very helpful.
0
 
LVL 39

Expert Comment

by:abel
ID: 24170424
Tx & glad it helped!
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

829 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