9.1.4. is-a vs. has-a (Day 2)

Another type of relationship between classes is the has-a relationship or association relationship. Use this when the object of one class contains a reference to one or more of another class. For example, a course can have many course periods associated with it as shown below. The 1 near the Course means that 1 course object is associated with the number shown near the other class. In this case it is * which means 0 to many. So one course is associated with 0 to many course periods.

../_images/assoc.png

Figure 3: A UML Class Diagram showing Association

In the code, the Course class has an array or ArrayList of CoursePeriod objects as an attribute inside it.

public class Course
{
   private ArrayList<CoursePeriod> periodList;
}

Alternatively, we could say that a CoursePeriod has a Course attribute inside it to hold the information about the Course. It is up to the programmer how to design these two classes depending on which type of association would be more useful in the program.

public class CoursePeriod
{
   private Course courseInfo;
   private int period;
}

Here is another example. Consider the class Student and Course and an APcourse. An APcourse is a special type of Course. Students are in Courses. What are the relationships between these classes? The UML diagram below shows the inherits (is-a) relationship between Course and APcourse and the associate (has-a) relationship between Class and Students.

../_images/APcourseUML.png

Figure 4: A UML Class Diagram for Student, Course, APcourse

coding exercise Coding Exercise

We can represent the diagram in Figure 4 in the code below. The Course class has an ArrayList of Student objects in it as the roster attribute. And an APcourse extends Course. What do you think the following code will print out?

9.1.5. is-a Substitution Test

If you aren’t sure if a class should inherit from another class ask yourself if you can substitute the subclass type for the superclass type. For example, if you have a Book class and it has a subclass of ComicBook does that make sense? Is a comic book a kind of book? Yes, a comic book is a kind of book so inheritance makes sense. If it doesn’t make sense use association or the has-a relationship instead.

Note

Only use inheritance when the child class is really a type of the parent class, otherwise use association.

exercise Check your understanding

    9-1-5: An online store is working on an online ordering system for Books and Movies. For each type of Published Material (books and movies) they need to track the id, title, date published, and price. Which of the following would be the best design?

  • Create one class PublishedMaterial with the requested attributes.
  • This will complicate the process of retrieving objects based on their type. Also if we need to add information that is specific to Book or Movie, it would be best if these were subclasses of PublishedMaterial.
  • Create classes Book and Movie and each class has the requested attributes.
  • This involves writing more code than is necessary (usually people copy and paste the shared code) and makes it harder to fix errors. It would be better to put common attributes and methods in the superclass PublishedMaterial and have Book and Movie be subclasses.
  • Create the class PublishedMaterial and have Book and Movie inherit from it all the listed attributes.
  • We will need to get objects based on their type so we should create classes for Book and Movie. They have common attributes so we should put these in a common superclass PublishedMaterial.
  • Create one class BookStore with the requested attributes.
  • The class name, BookStore, seems to imply the thing that keeps track of the store. This would be an appropriate class name for an object that handles the items in the Bookstore. However, for the published material, it would be better to use a superclass PublishedMaterial and subclasses for Books and Movies.
  • Create classes for PublishedMaterial, Books, Movies, Title, Price, ID, Authors, DatePublished
  • This is more classes than is necessary. Items such as Title, Price, ID, and DatePublished are simple variables that do not need a class of their own but should be attributes in a PublishedMaterial superclass, with Movies and Books as subclasses.

    9-1-6: An online site shows information about Books and Authors. What kind of relationship do these two classes have?

  • An is-a relationship. The Author class should be a subclass of the Book class.
  • Is an Author a type of Book? Or, does a Book have an Author associated with it?
  • An is-a relationship. The Book class should be a subclass of the Author class.
  • Is a Book a type of Author? Or, does a Book have an Author associated with it?
  • A has-a relationship. The Book class has an Author attribute.
  • A Book has an Author associated with it. Note that you could also say that an Author has many Books associated with it.

    9-1-7: What Java keyword is used to set up an inheritance relationship between a subclass and a superclass?

  • superclass
  • The parent class is the superclass, but this is not the Java keyword for declaring the parent class.
  • parent
  • The class you are inheriting from is called the parent or superclass, but this is not the Java keyword.
  • extends
  • The extends keyword is used to specify the parent class.
  • class
  • The class keyword is used to declare a class, but not the parent class.

9.1.6. groupwork Programming Challenge : Online Store

Working in pairs or groups, design an online store with classes for Store, ItemForSale, Book, Movie, and Author.

9.1.7. Summary

You have attempted of activities on this page
Next Section - 9.2. Inheritance and Constructors