I'm quite new to .NET programming. I know that .NET programming is 100% object oriented. An intriguing paragraph I read in a book about ASP.NET 4 states that
Inheritance is less useful than you might expect. In an ordinary application, most classes use containment and other relationships instead of inheritance, because inheritance can complicate life needlessly without delivering many benefits. Dan Appleman, a renowned .NET programmer, once described inheritance as “the coolest feature you’ll almost never use.”
I'm a little bit confused here and I need .NET programmers to tell me what should I take and what should I leave.
Edit
Please people understand my question, first the author didn't say literally "Inheritance in .NET is useless", that was my way to zip the hole question in some words to fit as a title. Second, the book is from Apress and its title is: "Beginning ASP.NET 4 in C# 2010" page 72.
I think the inheritance that's presented in most intro object-oriented academic classes is quite useless. You'll always see hierarchies for Animal or Shape, but most real problems don't look like that.
And both of these examples are broken quickly. Take a look at Linnaeus classification of species in biology or this article about Rectangle Shape to see what I mean. Any inheritance hierarchy will break down after a certain number of levels because leaf classes are getting further away from maintaining IS-A with the base class.
Any model represents a choice of what to leave in and what to exclude. Inheritance can certainly work for some models and not so well for others.
Examples of where inheritance works well are data structures and interfaces. An interface says that method signatures are inherited, leaving out implementation details. So if you can abstract what's common to all List or Set or Map implementations you've got something that provides structure but leaves implementation details to designers.
I'd say that inheritance works quite well in those cases.
Like most blanket statements, the one that you cited isn't universally true.
Inheritance is an OO concept, that varies very little in .NET than in any other OO language, save that .NET allows single-inheritance (for classes) only; which is also pretty common.
The author is right that inheritance generally is easy to use incorrectly, and that generally encapsulation is a better option, but this is not specific to .NET in any way.
There are many times when it makes perfect sense to use inheritance, and you shouldn't shy away from it; polymorphism etc being a prime example. But most standard "data in data out" code doesn't need polymorphism.
It is there, valuable - nay, essential (at least, to preserve sanity; you can of course avoid it if you like doing things the hard way). Feel free to ignore it or use it.
But make your own decision. Personally I think the author is being confusing and perhaps a bit short-sighted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With