Abstract Classes
Home ] Up ] Abstract Methods ] [ Abstract Classes ]

 

 

However, a class with one or more abstract methods must itself be declared abstract:

public abstract class Employee
{
    public abstract void raiseSalary(double byPercent);
    // ...
}

(The Java compiler will remind you if you forget!)

An abstract method promises that all non-abstract subclasses of this class will implement that abstract method.

Abstract methods act as placeholders for methods to be implemented in the subclasses.

A class contains abstract methods if any of the following is true:

  • It explicitly contains a declaration of an abstract method
  • It inherits an abstract method from its direct superclass, but does not implement that method.
  • "A direct superinterface of the class declares or inherits a method and the class neither declares it nor inherits a method that implements it. "(See later)

Only abstract classes may contain abstract methods. If a class contains abstract methods, but is not declared abstract, then a compile-time error occurs.

The Methods of Abstract Classes

Abstract classes do not have to contain only abstract methods. They may contain a mixture of abstract and concrete methods -- and in fact, usually do.

It is a good idea to push as much functionality as possible (as appropriate to that class) into the more abstract classes -- it allows you to reuse code more easily.

You may declare a class to be abstract, even if it does not contain any abstract methods. This prevents the class from being instantiated.

 

This page was last modified on 02 October, 2007