Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a list of any type that implements multiple interfaces?

Tags:

c#

generics

Consider the following class hierarchy:

public interface X { void Foo(); }

public interface Y { void Bar(); }

public class A : X, Y
{
    public void Foo() {}
    public void Bar() {}
}

public class B : X, Y
{
    public void Foo() {}
    public void Bar() {}
}

Is there any way I can define a list (or any generic type for that matter) which can contain both A's and B's, while allowing me to treat the contents of said list as both X and Y? I.e. something that will allow me to write something along the lines of this:

var list = ???
list.Add(new A());
list.Add(new B());
list.First().Foo();
list.Last().Bar();

Edit

Just to clarify, the types I'm currently dealing with are ObservableCollection<T> (A) and ReadOnlyObservableCollection<T> (B), where the interfaces I'm interested in are IList<T> (X) and INotifyCollectionChanged (Y). Clearly I'm unable to change their class hierarchy to accomodate my needs, so I need a different workaround.

like image 480
Steven Avatar asked Jan 13 '15 01:01

Steven


People also ask

Can you implement multiple interfaces?

A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

Is it possible to implement multiple interfaces in Java?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

What is interface can we implement multiple interfaces in one class?

C# allows that a single class can implement multiple interfaces at a time, and also define methods and variables in that interface.

Can a class inherit multiple interfaces?

A class or struct can implement multiple interfaces, but a class can only inherit from a single class.

What must a class implement to implement an interface?

A class that implements an interface must implement all the methods declared in the interface. Syntax: interface <interface_name>{ // declare constant fields // declare methods that abstract // by default.

Why create multiple interfaces in Java?

Creating an interface is a good way of grouping related functionalities. There are times when you need to bring related functionalities together into one class. being able to implement multiple interfaces gives you that flexibility. We’ll go over how to implement multiple interfaces and scenarios where it’s appropriate to do so.

What are some examples of functional interfaces in Java?

Runnable, ActionListener, Comparable are some of the examples of functional interfaces. Before Java 8, we had to create anonymous inner class objects or implement these interfaces.

What is the use of interface in it?

It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.


2 Answers

No, unless you declare another interface:

IAB : IA, IB {}

and make both classes implement it.

You could also implement your own collection class, something like List<IFirst, ISecond>, which would allow that.

like image 147
MarcinJuraszek Avatar answered Oct 18 '22 00:10

MarcinJuraszek


It is likely you don't want to modify the class A and B So a wrapper will do the job

public class XYWrapper : X, Y
{

  private dynamic InternalObject { get; set; }

  public void Foo() { InternalObject.Foo(); }
  public void Bar() { InternalObject.Bar(); }

  public static implicit operator XYWrapper(A value)
  {
    var instance = new XYWrapper();
    instance.InternalObject = value;
    return instance;
  }

  public static implicit operator XYWrapper(B value)
  {
    var instance = new XYWrapper();
    instance.InternalObject = value;
    return instance;
  }
}

So you use it in this way:

var list = new List<XYWrapper>();
list.Add(new A());
list.Add(new B());
list.First().Foo();
list.Last().Bar();
like image 6
Eric Avatar answered Oct 18 '22 01:10

Eric