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.
Sections 11.2.2 - 11.2.3 and 11.9 (pp. 347-349, 373-379) from the course textbook.
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.
The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
super
object and calls the parent/base/super class’ constructors (e.g. super()
).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.
For each of the following “class headers”, state the class from which it inherits.
The Cat class
public class Cat extends Mammal
The God class
public class God
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.
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.)