Wednesday, December 28, 2005

C# inheritance

The can be done in a number of programming methodologies. Now for something like the waterfall approach, inheritance is all planned out. With other more dynamic programming methodologies it's not so obvious, which methods should be overwritten and which shouldn't be.

Java is a language where if something is overwritten in the child the parent's version will automatically execute the child code's view. In C# the designers have decided to go for a literal approach when it comes to the language and inheritance. Much like C++ which is understandable considering it translated to IL as well as C# and VB.NET.
Take the following example. If the class car is derived from automobile and automobile has a method called TopSpeed. Now if the virtual and override keywords aren't used then if somebody assigns a car to type automobile the code for automobile will be executed when the method TopSpeed is executed.

I believe this is a case of bad language design. If a child class is to have a method with the same name as parent's method it should implicitly override the parent method. This begs the question if one is writing libraries in the infrastructure of a project that are to be used by developers, should all methods be made virtual? I believe if you wish to allow flexible extensibility by your programmers then a strong argument might be made for it.

No comments: