Using Generics and Inheritance to Simplify Development
page 3 of 9
by Brian Mains
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35721/ 50

Beware the Pitfalls

Before looking into the benefits, beware the potential pitfalls with inheritance and generics in combination with inheritance.  Inheritance can and sometimes does get out of hand, where a derived class may derive from five or more base classes.  While it may pay off for some people, it can quickly become a maintenance nightmare.  What if a property in the third base class changes, but should not change for the two-three other derived classes?  The solution may be to ignore the property or to shadow it and make it private.  What happens if a method changes its algorithm?  Will it break the inheritance chain or cause a problem for the other classes?

Sometimes, a different development structure (maybe the state/strategy pattern or simplifying the inheritance structure) is a better solution.  I know from personal experience the problems that can occur from deep chains of inheritance, as I have developed programs with the very problem I am talking about.  Note, however, I am not stating that inheritance is bad, as we shall soon see; I am just warning you to be careful how you plan your inherited structures and recommend keeping your inheritance chains short.

Generic base classes can cause a problem because the generic type of a base class makes the base class specific and you cannot take advantage of generic switching in some situations.  For instance, a class Entity that inherits from BusinessObject<EntityBase> does not inherit directly from BusinessObject, but BusinessObject<EntityBase> and, therefore, you may not be able to reference it dynamically in your application.


View Entire Article

User Comments

Title: Using Generics and Inheritance to Simplify Development   
Name: Meena
Date: 2007-10-26 6:32:53 PM
Comment:
This article help me a lot to learn generics.

Thanks
Title: Great Article!   
Name: Mohammad Azam
Date: 2007-09-28 3:18:52 PM
Comment:
Hi Brain,

Great article as always! Keep up the good work.
Title: Using Generics and Inheritance to Simplify Development   
Name: Brain
Date: 2007-05-12 1:40:08 PM
Comment:
Outstanding article. It was very informative. Thanks dude.
- Uday.D






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 4:36:06 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search