Java Reference
In-Depth Information
TIP: An Abstract Class Is a Type
You cannot create an object of an abstract class (unless it is actually an object of
some concrete descendent class). Nonetheless, it makes perfectly good sense to have
a parameter of an abstract class type such as
Employee
(as defined in Display 8.7 ).
Then, an object of any of the descendent classes of
Employee
can be plugged in for
the parameter. It even makes sense to have a variable of an abstract class type such as
Employee
, although it can only name objects of its concrete descendent classes.
■
An Abstract Class Is a Type
You can have a parameter of an abstract class type such as the abstract class
Employee
defined in Display 8.7. Then, an object of any of the concrete descendent classes of
Employee
can be plugged in for the parameter. You can also have variables of an abstract class type such
as
Employee
, although it can only name objects of its concrete descendent classes.
Self-Test Exercises
10. Can a method defi nition include an invocation of an abstract method?
11. Can you have a variable whose type is an abstract class?
12. Can you have a parameter whose type is an abstract class?
13. Is it legal to have an abstract class in which all methods are abstract?
14.
The abstract class
Employee
( Display 8.7 ) uses the method defi nitions from
Display 7.2 . After we did Display 7.2 , we later gave the following improved
version of
equals
:
public boolean
equals(Object otherObject)
{
if
(otherObject ==
null
)
return false
;
else if
(getClass() != otherObject.getClass())
return false
;
else
{
Employee otherEmployee =
(Employee)otherObject;
return
(name.equals(otherEmployee.name)
&& hireDate.equals(otherEmployee.hireDate));
}
}
Would it be legal to replace the version of
equals
for the abstract class
Employee
with this improved version?
(continued)