Association vs Inheritance

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 2: A UML Class Diagram showing Association

This would typically translate into a field in the Course class that has an array or list of CoursePeriod objects. The CoursePeriod class would have a field that is of type Course as shown below.

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

public class CoursePeriod
{
   private Course myCourse;
}

Substitution Test for Inheritance

If you aren’t sure if a class should inherit from another class ask yourself if you can substitute the child class type for the parent class 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.

Check your understanding

    10-2-1: A bookstore is working on an on-line ordering system. For each type of published material (books and movies) they need to track the id, title, author(s), date published, and price. Which of the following would be the best design?
  • (A) Create one class PublishedMaterial with the requested fields plus type
  • 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.
  • (B) Create classes Book and Movie and each class has the requested fields
  • 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 fields and methods in the superclass PublishedMaterial and have Book and Movie be subclasses.
  • (C) Create the class PublishedMaterial and have Book and Movie inherit from it all the listed fields
  • We will need to get objects based on their type so we should create classes for Book and Movie. They have common fields so we should put these in a common superclass PublishedMaterial.
  • (D) Create one class BookStore with the requested fields plus type
  • 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.
  • (E) Create classes for PublishedMaterial, Books, Movies, Title, Price, ID, Authors, DatePublished
  • This is more classes than is necessary. Items such as Title, Price, ID, Authors and DatePublished are simple variables that do not need a class of their own but should be fields in a PublishedMaterial superclass, with Movies and Books as subclasses.
    10-2-2: A movie theater has multiple showings of a movie each day. Each movie showing has a start time and location (theater number). What should the relationship be between the Movie class and the MovieShowing class?
  • (A) The MovieShowing class should be a subclass of the Movie class.
  • Is a movie showing a type of movie? Or, does a movie showing have a movie associated with it?
  • (B) The Movie class should be a subclass of the MovieShowing class.
  • Is a movie a type of movie showing? Or, does a movie showing have a movie associated with it?
  • (C) A MovieShowing has a movie associated with it, so it should have a Movie field.
  • A movie showing is not a type of movie and a movie is not a type of movie showing. A movie showing has a movie associated with it.
    10-2-3: What Java keyword is used to specify the parent class?
  • (A) superclass
  • The parent class is the superclass, but this is not the Java keyword for declaring the parent class.
  • (B) parent
  • The class you are inheriting from is called the parent or superclass, but this is not the Java keyword.
  • (C) extends
  • The extends keyword is used to specify the parent class.
  • (D) class
  • The class keyword is used to declare a class, but not the parent class.
    10-2-4: Question: Which of the following reasons for using an inheritance heirarchy are valid?
    1. Object methods from a superclass can be used in a subclass without rewriting or copying code.
    2. Objects from subclasses can be passed as arguments to a method that takes an argument of the parent type.
    3. Objects from subclasses can be stored in the same array of the parent type.
    4. All of the above
    5. None of the above
  • (A) V
  • In fact, all of the reasons listed are valid. Subclasses can reuse object methods written for superclasses without code replication, subclasses can be stored in the same array when the array is declared to be of the parent type, and objects of subclasses can passed as arguments of the superclass type. All of which make writing code more streamlined.
  • (B) IV
  • All of these are valid reasons to use an inheritance heirarchy.
  • (C) I and II
  • III is also valid. In some cases you might want to store objects of subclasses together in a single array declared to be of the parent type, and inheritance allows for this.
  • (D) I and III
  • II is also valid. In some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass objects of the subclasses to the same method if it takes an argument of the parent type, instead of writing individual methods for each subclass.
  • (E) I only
  • I and III are also valid, in some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass all the subclasses to the same method instead of writing individual methods for each subclass and you might want to store subclasses together in a single array, and inheritance allows for this.
Next Section - The Equals Method