Wednesday, August 16, 2006

Inheritance

Logging frameworks like Log4J allow us to send logs to a multitude of destinations. The code responsible for logging to a destination is usually encapsulated into separate classes.






Without inheritance



With inheritance




When to use inheritance:

  • IS-A relationship
    • An Employee is a Person
    • A BumperSale is a Sale
    • A Square is a Shape
  • IS-LIKE-A relationship

But if the relationship between them is HAS-A (eg: Car has an Engine), then we use composition instead of inheritance.


Reflections:


What is the relationship between the following objects?

  • Bathroom - Bathtub
  • Car - Engine
  • Person - Professor
  • Vehicle - Car

Note: This post was originally posted on my blog at http://www.adaptivelearningonline.net

No comments: