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.
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.
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).
C# allows that a single class can implement multiple interfaces at a time, and also define methods and variables in that interface.
A class or struct can implement multiple interfaces, but a class can only inherit from a single class.
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.
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With