Chapter 14 - Inheritance Flashcards
What class is a superclass for all other classes?
The Object class
What is unique about the Object class compared to other superclasses?
You don’t have to specifiy it as a superclass, it is automatically a superclass.
What are the two most important Object methods?
equals and toString
If a class doesn’t have it’s own equals method, but an object from that class calls the equals method what happens?
It inherits and uses the Object class’s equals method.
What does the Object class’s equal method return if two reference variables that are being compared point to the same object?
The method returns true.
What operator works the same as the Object class’s equals method?
==
Why is the Object class’s equals method usually not good enough?
Because you usually want to compare the contents of two objects rather than just whether two reference variables point to the same object.
How do you compare the contents of two objects using the Object class’s equals method?
You need to have an equals method in the object’s class definition that compares the contents of the two objects.
The equals method is built into a lot of Java’s API classes?
T or F
True
What method in the Object class returns a string that’s a concatenation of the calling object’s class name, an @sign, and a sequence of digits and letters (called a hashcode)?
The toString method
What does toString prefix if a clas is stored in a package?
The class name with the class’s package.
Since retrieving the class name, an @ sign, and a hashcode is usually worthless, we want to almost always avoid calling the Object class’s toString method. What do we call instead?
An overriding toString method
In general, what should a toString method return?
A string that descirbes the calling object’s contents.
Since retrieving the contents of an object is a common need, what habit should a programmer get into?
Proving a toString method for most programmer-defined classes.
What should a programmer’s toString method typically do?
Simply concatenate the calling object’s store data and return the resulting string.