Java Reference
In-Depth Information
Abstract Method
An abstract method serves as a placeholder for a method that will be fully defined in a
descendent class. An abstract method has a complete method heading with the addition of
the modifier abstract . It has no method body but does end with a semicolon in place of a
method body. An abstract method cannot be private.
EXAMPLES
public abstract double getPay();
public abstract void doSomething( int count);
abstract class
A class that has at least one abstract method is called an abstract class and, in Java,
must have the modifier abstract added to the class heading. The redefined, now
abstract, class Employee is shown in Display 8.7 .
An abstract class can have any number of abstract methods. In addition, it can have,
and typically does have, other regular (fully defined) methods. If a derived class of an
abstract class does not give full definitions to all the abstract methods, or if the derived
class adds an abstract method, then the derived class is also an abstract class and must
include the modifier abstract in its heading.
In contrast with the term abstract class , a class with no abstract methods is called a
concrete class .
concrete class
Display 8.7
Employee Class as an Abstract Class (part 1 of 2)
1 /**
2 Class Invariant: All objects have a name string and hire date.
3 A name string of "No name" indicates no real name specified yet.
4 A hire date of January 1, 1000 indicates no real hire date specified
yet.
5 */
6 public abstract class Employee
7 {
8
The class Date is defined in Display 4.13, but the
details are not relevant to the current discussion
of abstract methods and classes. There is no
need to review the definition of the class Date .
private String name;
9
private Date hireDate;
10
public abstract double getPay();
11 public Employee()
12 {
13 name = "No name";
14
hireDate = new Date("January", 1, 1000);
//Just a placeholder.
15 }
16 public boolean samePay(Employee other)
17 {
18
if (other == null )
(continued)
 
Search WWH ::




Custom Search