One question that is often raised by beginning modelers is why we don't use an inheritance relationship to relate the Course and Section classes, rather than using a simple association as we've chosen to do. On the surface, it does indeed seem tempting to want Section to be a derived class of Course, because all of the attributes listed for a Course—courseNo, courseName, and credits—also pertain to a Section; so, why wouldn't we want Section to inherit these, in the same way that Student and Professor inherit all of the attributes of Person? A simple example should quickly illustrate why inheritance isn't appropriate.
Let's say that, because Beginning Object Concepts is such a popular course, the university is offering three sections of the course for the Spring 2004 semester. So, we instantiate one Course object and three Section objects. If Section were a derived class of Course, then all four objects would carry courseNo, courseName, and credits attributes. Filling in the attribute values for these four objects, as shown in Table 10-3, we see that there is quite a bit of repetition in the attribute values across these four objects: we've repeated the same courseName, courseNumber, and creditValue attribute values four times! That's because the information contained within a Course object is common to, and hence describes, numerous Section objects.
Attribute Name |
Attribute Values for the Course Object |
||
courseName |
Beginning Object Concepts |
||
courseNumber |
OBJECTS 101 | ?/td> | ?/td> |
creditValue |
3 | ?/td> | ?/td> |
Attribute Name |
Attr. Values for Section Object #1 |
Attr. Values for Section Object #2 |
Attr. Values for Section Object #3 |
courseName |
Beginning Object Concepts |
Beginning Object Concepts |
Beginning Object Concepts |
courseNumber |
OBJECTS 101 |
OBJECTS 101 |
OBJECTS 101 |
creditValue |
3 |
3 |
3 |
students Registered |
(To be determined) |
(To be determined) |
(To be determined) |
instructor |
Reference to professor X |
Reference to professor Y |
Reference to professor Z |
semester Offered |
Spring 2004 |
Spring 2004 |
Spring 2004 |
dayOfWeek |
Monday |
Tuesday |
Thursday |
timeOfDay |
7:00 p.m. |
4:00 p.m. |
6:00 p.m. |
classroom |
Hall A, Room 123 |
Hall B, Room 234 |
Hall A, Room 345 |
To reduce redundancy and to promote encapsulation, we should eliminate inheritance of these attributes, and instead create only one instance of a Course object for n instances of its related Section objects. We can then have each Section object maintain a handle on the common Course object so as to retrieve these shared values whenever necessary. This is precisely what we've modeled via the one-to-many offered as association.
Whenever an instance of some class A encapsulates information that describes numerous instances of some other class B (such as Course does for Section), we refer to the information contained by the A object (Course) as metadata relative to the B objects (Sections).