Java Reference
In-Depth Information
Display 8.7 Employee Class as an Abstract Class (part 2 of 2)
19 {
20 System.out.println("Error: null Employee object.");
21 System.exit(0);
22 }
23
//else
24
return ( this .getPay() == other.getPay());
25 }
All other constructor and other method definitions are exactly the same as in Display 7.2.
In particular, they are not abstract methods.
26 }
Abstract Class
An abstract class is a class with one or more abstract methods. An abstract class must
have the modifier abstract included in the class heading, as illustrated by the example.
EXAMPLE
public abstract class Employee
{
private String name;
private Date hireDate;
public abstract double getPay();
...
PITFALL: You Cannot Create Instances of an Abstract Class
You cannot use an abstract class constructor to create an object of the abstract class.
You can only create objects of the derived classes of the abstract class. For example,
with the class Employee defined as in Display 8.7, the following would be illegal:
Employee joe = new Employee(); //Illegal because
//Employee is an abstract class .
But, this is no problem. The object joe could not correspond to any real-world
employee. Any real-world employee is either hourly or a salaried. In the real world,
one cannot be just an employee. One must be either an hourly employee or a
salaried employee. Still, it is useful to discuss employees in general. In particular,
we can compare employees to see if they have the same pay, even though the way of
calculating the pay might be different for the two employees.
 
 
Search WWH ::




Custom Search