Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherently-Implemented Interfaces

I have often wanted to create a list of objects where each object must implement a number of interfaces. For example, I'd like to do something similar to the following:

List<T> where T : IConvertible, IComparable _myList;

Another option I considered was to create a third interface that implements these two so that any object that implements these two interfaces inherently implements mine.

public interface IConvertibleAndComparable
    : IConvertible, IComparable { }

List<IConvertibleAndComparable> _myList;

With this I would be able to add any object that implements both IConvertible and IComparable, including double and int, as well as my own objects. Explicitly implementing IConvertibleAndComparable is not required since it does not add any new functionality beyond the interfaces in inherits.

I understand that the first snippet is illegal and the second, while legal, does not do what I want. Is it possible to achieve what I am trying to do? If not, would either of these be a candidate for a future C# feature?

(Note: This would be legitimate application for empty interfaces.)

Edit

In a more general sense, I'd like to perform one of the following:

private MyGenericClass<T> where T : IA, IB, ... _myClass;

where I can declare all of the restrictions on T that I need, or

public interface IMyCombinedInterface : IA, IB, ... {}
private MyGenericClass<IMyCombinedInterface> _myClass;

where any type that implements IA, IB, and ... inherently (or implicitly) implements IMyCombinedInterface (only when IMyCombinedInterface doesn't explicitly declare new functionality).

like image 663
gregsdennis Avatar asked Oct 17 '12 15:10

gregsdennis


People also ask

Which inheritance is implemented only with interfaces?

Multiple inheritance in Java by interface If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.

Is interface implementation inherited?

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.

Can you implement more than one interface?

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.

Are interfaces inherited Java?

Also, it is possible for a java interface to inherit from another java interface, just like classes can inherit from other classes. You specify inheritance using the extends keyword. Inheritance will be further discussed below. But unlike classes, interfaces can actually inherit from multiple interfaces.


1 Answers

You can, as a workaround, do a kind of superposition wrapper, and store it in the list. Look here for the idea.

To your example you could do:

public class Junction
{
    public IConvertible Convertible { get; private set; }
    public IComparable Comparable { get; private set; }
    private Junction() { }
    public static Junction Create<T>(T value) where T : IConvertible, IComparable
    {
        return new Junction
        {
            Convertible = value,
            Comparable = value
        };
    }
}

And then add Junctions to your list:

var l = new List<Junction>();
l.Add(Junction.Create(1));
like image 157
Jordão Avatar answered Nov 02 '22 23:11

Jordão