Defining abstract Methods and Classes

Purpose:

To declare that a method has no default implementation in superclass or that a class should serve as a superclass only and not be instantiated.

Mechanics:

abstract <other modifiers> <return type> <method name>();

abstract <other modifiers> class <class name> {
/* Instance variables declared and methods defined here. */
}

Example:

abstract public void animate();

abstract public class Shape {
/* Instance variables declared and methods defined here. */
}

Usage:

  • abstract is one of several possible modifiers that can appear anywhere in the space-separated list of modifiers.
  • A method which is declared abstract is not followed by a definition; instead, the declaration line simply ends in a semicolon.
  • Any class which contains at least one abstract method, inherits an abstract method, but does not define it, or implements an interface, but does not define every method declared in that interface, must be declared abstract.
  • A class can also be declared abstract to prevent instantiation.

Restrictions:

A class or method cannot be declared as both abstract and final. A variable cannot be declared abstract.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu