In C# you have to mark method as virtual to make it possible to override. Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to be overridden), since most likely you don't know in what way your class can be inherited?
No. If the language designers thought that virtual should have been the default then it would have been the default.
Overridablility is a feature, and like all features it has costs. The costs of an overrideable method are considerable: there are big design, implementation and testing costs, particularly if there is any "sensitivity" to the class; virtual methods are ways of introducing untested third-party code into a system and that has a security impact.
If you don't know how you intend your class to be inherited then don't publish your class because you haven't finished designing it yet. Your extensibility model is definitely something you should know ahead of time; it should deeply influence your design and testing strategy.
I advocate that all classes be sealed and all methods be non-virtual until you have a real-world customer-focussed reason to unseal or to make a method virtual.
Basically your question is "I am ignorant of how my customers intend to consume my class; should I therefore make it arbitrarily extensible?" No; you should become knowledgable! You wouldn't ask "I don't know how my customers are going to use my class, so should I make all my properties read-write? And should I make all my methods read-write properties of delegate type so that my users can replace any method with their own implementation?" No, don't do any of those things until you have evidence that a user actually needs that capability! Spend your valuable time designing, testing and implementing features that users actually want and need, and do so from a position of knowledge.
final
by default. – Madrienefinal
fields, I know that he talks about them. But I think he also talks about final methods in Effective Java. However, I may misremember this. I’m sure though that he mentioned it elsewhere then – maybe in one of his talks. – Madrienefinal
, while in reality it is not. Some are used to show that evenfinal
doesn't help immutability (notorious j.u.Date), somefinal
are used as "awful candidate for default serialized form" - StringList and so on.No class in collection framework is final, either.Admittedly, I'm no great fan of his, I haven't followed his talks but unless you have a source for Java guru advocating "final methods by default" I'd not accept it. – Mythify