Blog

horizontal line graphic

Subscribe to Kevin Southworth's Blog  Subscribe to my RSS feed | Categories | Search

Inheritance or Containment?

Wednesday, January 24, 2007 @ 2:03 AM :: 306 Views :: 0 Comments ::
Categories: Software Development

I''ve been reading Code Complete lately (which is a great book so far), and the author, Steve McConnell, has some good general advice about when to use Inheritance and when to use Containment when designing your classes:

  • If multiple classes share common data but not behavior, create a common object that those classes can contain
  • If multiple classes share common behavior but not data, derive them from a common base class that defines the common routines
  • If multiple classes share common data and behavior, inherit from a common base class that defines the common data and routines
  • Inherit when you want the base class to control your interface; contain when you want to control your interface

Rating
Comments
Currently, there are no comments. Be the first to post one!
Click here to post a comment