Virtual versus Final Methods
When defining a method for the first time, you can state whether a sub-class can override it.
In Java the default allows overriding, in C# and Kotlin the default prevents overriding.
To prevent overrinding in Java, use the
final keyword. To allow overriding in C# use
virtual and in Kotlin use
Which default is better?
That depends on what you consider most important. Final methods are safer, as it prevents sub-classes doing something that breaks the super class. Final methods are quicker, because there more optimisations can be made on final methods. Open methods are more flexible though. So there is a trade-off between flexibility on the one hand and speed and robustness on the other.
However, I think all three languages have missed a third option. Instead of having a default, and an optional keyword, why not force all methods to state their preference. Kotlin does this with its