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

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
This article help me a lot to learn generics.

Title: Great Article!   
Name: Mohammad Azam
Date: 2007-09-28 3:18:52 PM
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
Outstanding article. It was very informative. Thanks dude.
- Uday.D

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

©Copyright 1998-2024  |  Page Processed at 2024-04-13 12:55:23 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search