Interface methods
Interface methods are declared using interface-method-declarations: interface-method-declaration: The attributes, return-type, identifier, and formal-parameter-list of an interface method declaration have the same meaning as those of a method declaration in a class (§10.6). An interface method declaration is not permitted to specify a method body, and the declaration therefore always ends with a semicolon. Each formal parameter type of an interface method must be input-safe (§13.1.3.1), and the return type must be either void or output-safe. Furthermore, each class type constraint, interface type constraint and type parameter constraint on any type parameter of the method must be input-safe. These rules ensure that any covariant or contravariant usage of the interface remains typesafe. For example, interface I<out T> { void M<U>() where U: T; } is illegal because the usage of T as a type parameter constraint on U is not input-safe. Were this restriction not in place it would be possible to violate type safety in the following manner: class B {} This is actually a call to C.M<E>. But that call requires that E derive from D, so type safety would be violated here.
|