Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are interfaces not able to be marked as sealed?

Tags:

public sealed interface IMyInterface { } 

Gives "The modified 'sealed' is not valid for this item"

I can understand in some ways that an interface must be descendable otherwise the class cannot implement it.

But why can I not specify that an interface should not have a sub interface defined or is there a way, just not with sealed?

Edit

I should have made some effort to explain why I would want this. I often see interface inheritence chains where the dev should be using composition instead. Sealed is ideal for this in classes and I wondered if there was a way to enforce the same for interfaces. As unnessasary inheritence makes it harder to refactor and maintain in my opinion.

Edit 2

On reflection of the comments and posts, interface inheritence trees can't be anywhere near as complex as object inheritence trees. As when you are deriving from another interface IX all you are saying is "must also implement IX". And preventing that has no benefit.

like image 918
weston Avatar asked Mar 27 '12 13:03

weston


People also ask

Can an interface be declared as sealed?

Since Java 15, now a class or interface can be declared sealed class or sealed interface using the modifier sealed . It is a preview feature in Java 15. A sealed class or interface restricts which other classes or interfaces may extend or implement them.

Can an interface be declared as sealed in C#?

An interface itself cannot be declared as sealed but, it can have both virtual and sealed methods in C# 8.

When would you use a sealed interface?

Summary. Sealed classes and interfaces should be used to represent restricted hierarchies. They make it easier to handle each possible value and, as a result, to add new methods using extension functions. Abstract classes leave space for new classes to join this hierarchy.

Why do we declare class as sealed?

Sealed class is used to stop a class to be inherited. You cannot derive or extend any class from it. Sealed method is implemented so that no other class can overthrow it and implement its own method.


Video Answer


2 Answers

The purpose of sealing a class, or a virtual method of a class, is to lower your costs. Designing for inheritance is expensive, and if you do not do it correctly, it is dangerous. There are security, correctness and robustness consequences to improperly designing for inheritance, so if you do not intend to design for inheritance, it is wise to seal your class and thereby avoid the costs associated with designing for inheritance.

Classes need to be designed for inheritance because they have implementation details. Interfaces have no implementation details. There is no cost associated with interfaces being inheritable. And therefore there is no incentive to add the feature of allowing interfaces to be sealed.

like image 91
Eric Lippert Avatar answered Dec 21 '22 22:12

Eric Lippert


It would just be confusing. Using the standard syntax, it would imply that you cannot implement the interface. Also, interfaces don't contain any functionality or fields, so there is no practical use in sealing it. An interface is more or less a contract.

Sealing an interface from "interface inheritance" would not do anything, since people could just implement your interface and the other one that would have inherited your interface.

like image 32
Botz3000 Avatar answered Dec 21 '22 22:12

Botz3000