Java Reference
In-Depth Information
ways to check for equality, so the purposes of your program will help you determine an appropriate
implementation. Two cars might be equal if they have the same vehicle identification number (the
same exact car), or you might consider them equal if the make, model, and year are the same (the
same kind of car).
The
finalize()
method is used as part of garbage collection. There is a brief section on this topic
later in this chapter. It is worth noting that
Object
is default
finalize()
method doesn't do any-
thing, but it can be overridden to clean up loose ends when an object becomes garbage. Oracle notes
that when and even if the
finalize()
method is called is uncertain, so it should not be relied upon
to close resources.
The
getClass()
method cannot be overridden. It will always return a
Class
object that you can use
to get information about the class, such as the class name, the superclass, or any interfaces it imple-
ments. The
Class
class has dozens of methods you can use to learn more about a class.
The
hashCode()
method returns a value representing the object's memory address by default. By defi-
nition, if two objects are equal, their hashcode must also be equal. Therefore, when you override the
equals()
method, you must also override the
hashCode()
method to maintain this relationship.
The
toString()
method returns a String representation of an object. The String representation
depends on the object, so you should consider overriding this method in your own classes. The
toString()
method is the method called when you print an object, so if you properly override the
toString()
method, you can easily print objects in a format useful to you and your program.
abstract classes and methods
Abstract classes cannot be instantiated. They are used as superclasses when you want all instances
to belong to a subclass, but the subclasses share attributes or methods that are better defined in a
superclass. For example, you might have an abstract class called
Shape
with several subclasses, like
Rectangle
,
Circle
, and
Triangle
. It might not make sense to be able to create a
Shape
without
a more specific type, so you can make
Shape
abstract and create all shape objects as members of
a subclass. The keyword
abstract
is used in the class declaration, like
public abstract class
Shape { }
.
Abstract classes can also have implemented methods and abstract methods. Abstract methods are
declared without an implementation. In the
Shape
example, you might declare a
calculateArea()
method in the
Shape
class, but allow each subclass to implement it in its own way, since each type
of shape has a different area formula. All abstract methods must eventually be implemented in one
of the subclasses. All classes that contain an abstract method must be abstract themselves. Abstract
methods are declared using the
abstract
keyword, but instead of opening curly brackets, the dec-
laration is completed with a semicolon, like
public abstract double calculateArea();
. The
classes for
Shape
and
Circle
are shown as examples for the abstract class and method syntax.
public abstract class Shape {
private String color;
public Shape(String color) {
this.setColor(color);
}
Search WWH ::
Custom Search