How to return subtype in overridden method of subclass in C#?

Unfortunately no, covariant return types aren’t supported in C# for method overriding. (Ditto contravariant parameter types.) If you’re implementing an interface you can implement it explicitly with the “weak” version and also provide a public version with the stronger contract. For simple overriding of a parent class, you don’t have this luxury I’m afraid 🙁 … Read more

inheritance vs. composition for testability

I believe that the more you start to develop using design patterns, you’ll find more and more often where composition is going to be favored over inheritance. I actually believe in the Head First: Design Patterns book that “Favor Composition Over Inheritance” is one of the primary design principles. Your example of being able to … Read more

Should I use polymorphism in javascript?

As said, JavaScript is a dynamic typed language based on prototypal inheritance, so you can’t really use the same approach of typed languages. A JS version of what you wrote, could be: function Shape(){ throw new Error(“Abstract class”) } Shape.prototype.printSurface = function () { throw new Error (“Not implemented”); } function Rect() { // constructor; … Read more