C++

Being an Animal, a cow must eat any food

In C++, it is illegal to restrict the food type of a cow to PlantFood. You can not redefine the input interface of a method in a subclass.

Well, if the fact is that a cow eats plant food only, how do I write code for this fact to protect a cow to eat things that they won't to eat?

The answer of C++ is: a cow should be able to decide what kind of food they want. Why don't you let a cow to check the type of the food before they swallow it? Say:

The C++ solution is called invariance plus run-time type check. Invariance means that input interface of a method (member function) defined in a subclass should be invariant, or in other word, identical to the interface defined in its superclass.

Isn't that an ostrich policy? We cannot attempt to avoid the danger by refusing to face it. We are fortunate that a cow has a wisdom to chose its own food. But how about a car? Imagine that the manufactory writes the following specification in the car owner's manual:

and some people coming from a corner of the world where coal is the only fuel they use in their daily life! The manufactory's specification is quite misleading.

Interface specification plays an important role in software systems. If the fact requires a precise interface, the language should be able to provide a suitable specification that does not mislead the interface user.