Overview

In this reading we will wrap up our discussion of creating classes by exploring the concept of inheritence. Inheritance allows us to create new classes that are similar in nature without having to rewrite each class from scratch.

Java has an intricate inheritance model that takes some time to wrap your head around. While we won’t be going into extreme depth on this model, the time we spend will allow us to structure our programs in a logical way that cuts down on unnecessary code duplication.

Required Reading

Sections 11.2.2 - 11.2.3 and 11.9 (pp. 347-349, 373-379) from the course textbook.

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Describe what it means for one class to inherit from another.
  2. Given a class definition, identify which class it inherits from.
  3. Describe the process by which Java searches for a field or method in a given object and relate this to how you override methods in an derived class.
  4. Write a Java expression that invokes a method in the base class or invokes one of the base class’ constructors.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Read, write, and trace code that involves the use of a super object and calls the parent/base/super class’ constructors (e.g. super()).

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. For each of the following “class headers”, state the class from which it inherits.

  2. Assume that we create a ConfusedTurtle object as follows.

    World w = new World();
    ConfusedTurtle satty = new ConfusedTurtle(w);

    Suppose I were to call the hashCode method as follows.

    int hc = satty.hashCode(); // get the hash code for satty

    Describe the locations where Java looks for the hashCode method and the order in which it looks in these locations.

  3. Suppose that we have a class named Kitten that inherits from the Cat class. Write a Kitten method named cry that simply call’s the Cat class’ meow method 5 times. (Note: Use a loop to avoid code duplication.)