Java Reference
In-Depth Information
3.3. Inheriting and Redefining Members
When you extend a class you can both add new members to a class and
redefine existing members. Exactly what effect redefining an inherited
member has depends on the kind of member. You'll learn about field and
method members here, but we defer discussion of nested members until
3.3.1. Overriding
In our new
ColorAttr
class we have both
overridden
and
overloaded
the
instance method
setValue
:
•
Overloading
a method is what you have already learned: providing
more than one method with the same name but with different sig-
natures to distinguish them.
•
Overriding
a method means replacing the superclass's implement-
ation of a method with one of your own. The signatures must be
identicalbut the return type can vary in a particular way, as dis-
cussed below.
Overloading an inherited method simply means that you have added a
new method, with the same name as, but a different signature from, an
inherited method. In
ColorAttr
we have gone from having one
setValue
method to having two overloaded forms of the method.
public Object setValue(Object newValue) {
// ...
}
public ScreenColor setValue(ScreenColor newValue) {
// ...
}