Class Relationships Exercises

  < Previous  Next >
  1. In Java, we use inheritance for "is-a" relationships. Containment, on the other hand, indicates that one object has another object. These are "has-a" relationships.
    For the following relationships, determine if they are a "is-a" or a "has-a" relationship:
    • bicycle - vehicle
    • car - engine
    • pizza - topping
    • food - pizza
    • dog - mammal
  2. Share with someone else 3 other examples for each of the following relationships:
    • Inheritance ("is-a" relationship)
    • Containment ("has-a" relationship)
  3. public class PersonInfo {
       protected String firstName;
       protected String birthdate;
    
       ...
    }
    
    public class ChildInfo extends PersonInfo {
       private String schoolName;
    
       ...
    }
    
    public class MotherInfo extends PersonInfo {
       private String spousename;
       private ChildInfo children1;
       private ChildInfo children2;
       private ChildInfo children3;
      ...
    }
    
    1. Draw the relationships for the classes above using UML. If a class inherits from another class, draw an open arrow pointing to the base class from the derived class. If a class has another class as a variable, then draw a dashed line with an pointed arrow from the class that has the variable to the class that is used. image
    2. Label each arrow with the type of relationship: image
      1. "is-a"
      2. "has-a"