Theoretically it is sound to override methods of a parent in a subclass with a method where the parameters are supertypes of the parameters in the parent class, like:
class T
def foo(s: String) = ...
class S
override def foo(a: Any) = ...
Which programming languages support this "feature" and how do they solve issues like the possibility that a single method of a subclass can override multiple methods of a parent class:
class T
def foo(s: String) = ...
def foo(i: Int) = ...
class S
override def foo(a: Any) = ...
(Assuming that String
and Int
subtypes of Any
.)
(Incomplete answer) If we take the example of OCaml, it answers the title of your question: this a "statically-typed programming language with inheritance where method parameters are contravariant".
As for the second part of your question ("how do they solve issues like..."), the Gordian knot is cut by not allowing overloading. (Overloading means having two functions with the same name but different argument types, which makes type inference a lot harder).
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