Associated Classes - using objects of one class within another
Archive - Originally posted on "The Horse's Mouth" - 2012-07-21 14:47:06 - Graham Ellis
Wheh you're designing an object oriented class, you'll often use other objects within it. For example, you may design a Ferry object and within it use a Time object (for when it's scheduled to leave, perhaps): Ferry *mersey = new Ferry(new Time(17,45),14);
There's really no special syntax involved with this - so this is just a short article to remind you that, yes, you can do this, indeed you're encouraged to do so as it makes for more robust code and cleaner calling with objects being passed around within objects. You'll sometimes here the objects within being referred to as "associated objects" or perhaps "contained objects"; you need to remember that this is nothing to do with subclasses, inheritance, and polymorphism - that's a whole different way that classes can be related to other classes.