I was looking at the declaration of List<T>
and saw that it implements IList<T>
, ICollection<T>
and IEnumerable<T>
(among others).
Then I went to look the definition of IList<T>
and saw that it implements ICollection<T>
and IEnumerable<T>
.
What's the point of an interface implementing another interface if they work just as "contracts" and we write no real code at them?
Is this implementation cumulative? If it is, since IList<T>
implements ICollection<T>
and IEnumerable<T>
, List<T>
shouldn't implement only IList<T>
?
Sorry if my question is confusing, I'm a litte bit puzzled right now.
An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. The following Sports interface is extended by Hockey and Football interfaces.
An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends.
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.
But unlike classes, interfaces can actually inherit from multiple interfaces. This is done by listing the names of all interfaces to inherit from, separated by comma. A class implementing an interface which inherits from multiple interfaces must implement all methods from the interface and its parent interfaces.
What's the point of an interface implementing another interface if they work just as "contracts" and we write no real code at them?
Interfaces support inheritance and facilitate polymorphism in the same way that classes do. An IList<T>
is also an ICollection<T>
, in the same way that a TextBox
is a Control
.
Is this implementation cumulative? If it is, since IList implements ICollection and IEnumerable, List shouldn't implement only IList?
As others have pointed out, this is mostly for readability and maintainability. Yes, inheritance is cumulative. But, when a class implements multiple interfaces that contain members with the same signature, it only needs to define one member for all implementations. Explicit interface implementation may be used to explicitly define a member for a given implementation.
At the very least it reduces redundancy.
public interface INamedObject { string Name { get; } }
public interface IValuedObject<T> { T Value { get; } }
public interface INamedValuedObject<T> : INamedObject, IValuedObject<T> { }
Is better design than:
public interface INamedObject { string Name { get; } }
public interface IValuedObject<T> { T Value { get; } }
public interface INamedValuedObject<T> {
string Name { get; }
T Value { get; }
}
Also, in the latter case, notice that an INamedValuedObject<T>
is now neither an INamedObject
nor an IValuedObject<T>
.
You can read the former case as "Something that is an INamedValuedObject<T>
is something that is an INamedObject
, an IValuedObject<T>
, and also has the following elements".
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