Exercise 3.5: Write a new extended class that benchmarks something else, such as how long it takes to run a
loop from zero to some passed-in parameter.
Exercise 3.6: Change Vehicle so that it has an EnergySource object reference, which is associated with
the Vehicle in its constructor. EnergySource must be an abstract class, because a GasTank object's
measure of fullness will differ from that of a Battery object. Put an abstractempty method in
EnergySource and implement it in GasTank and Battery classes. Add a start method to Vehicle
that ensures that the energy source isn't empty.
3.8. The Object Class
The Object class is the root of the class hierarchy. Every class directly or indirectly extends Object and so
a variable of type Object can refer to any object, whether a class instance or an array. For example, the
Attr class can hold an attribute of any type, so its value field was declared to be of type Object. Such a
class cannot hold primitive types directly, but can hold references to the associated wrapper classsee Chapter
8.
The Object class defines a number of methods that are inherited by all objects. These methods fall into two
categories: general utility methods and methods that support threads. Thread support is covered in Chapter 14.
This section describes the utility methods and how they affect classes. The utility methods are:
public booleanequals(Object obj)
Compares the receiving object and the object referenced by obj for equality,
returning true if they have the same value and false if they don't. If you
want to determine whether two references refer to the same object, you can
compare them using == and !=. The equals method is concerned with
value equality. The default implementation of equals in Object assumes
that an object is equal only to itself, by testing if this ==obj.
public inthashCode()
Returns a hash code for this object. Each object has a hash code for use in
hashtables. The default implementation returns a value that is usually
different for different objects. It is used when storing objects in hashed
collections, as described in Chapter 21.
protected Objectclone()throws CloneNotSupportedException
Returns a clone of this object. A clone is a new object that is a copy of the
object on which clone is invoked. Cloning is discussed in the next section.
public final Class<?>getClass()
Returns the type token that represents the class of this object. For each class
T there is a type token Class<T> (read as "class of T") that is an instance of
the generic class Class described in "The Class Class" on page 399. When
invoked on an instance of Object this method will return an instance of
Class<Object>; when invoked on an instance of Attr it will return an
instance of Class<Attr>, and so forth.
protected voidfinalize()throws Throwable