Containment, Associative Objects, Inheritance, packages and modules
Archive - Originally posted on "The Horse's Mouth" - 2010-04-30 08:15:32 - Graham Ellis
Containment is where one object contains other objects - and it happens all over the place. A "town" object may, in a program, contain a number of "hotel" objects, a "location" object, zero or more "leisure" objects, "public transport hub" objects and so on.
Containment is different to inheritance - it's not the same thing at all. Inheritance is where one object has its code and behaviors based on the code of another - for example, the "leisure" object I mentioned in my example above could in fact be subtyped into "swimming pool" objects, "cinema" objects, "fitness centre" objects, and so on.
It's often sensible to bundle together a number of classes - where they'll be maintained by the same person or team, and used in conjunction with each other in the same applications. Depending on the programming language that you're using, you'll use the term Packages or Modules for such grouping of classes. And a package or module type relationship is different to a containment relationship, which is different to an inheritance relationship. It might be that our "leisure" and "public transport hub" objects are in the same module, but that does not imply inheritance, nor containment.
Design MATTERS - if you're going to make a new journey, you'll start by checking on the transport options and route rather than just getting in your car and driving randomly - and so it should be with your object / application design. By developing in a modular way, with frequent testing at each level, and thinking about the data, you should help yourself produce a robust, tailorable system - and diagrams that show which classes inherit from which, and what objects are contained in which, will help you think clearly from the start. Whether you go the whole way to using UML and / or extreme programming techniques will depend on you - and on the size and complexity of the project.
Containment and Inheritance are very much OO concepts, and for newcomers to OO, we cover them on our Python, Java, C++ and Ruby courses. For delegates learning Perl or PHP (which can be used in an OO or non-OO way), we cover the OO principles on specific courses - see [OO PHP] and [Perl for larger projects].