Defining
abstract Methods and
ClassesPurpose:
- 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:
abstractis one of several possible modifiers that can appear anywhere in the space-separated list of modifiers.- A method which is declared
abstractis not followed by a definition; instead, the declaration line simply ends in a semicolon. - Any class which contains at least one
abstractmethod, inherits anabstractmethod, but does not define it, or implements an interface, but does not define every method declared in that interface, must be declaredabstract. - A class can also be declared
abstractto prevent instantiation.
Restrictions:
- A class or method cannot be declared as both
abstractandfinal. A variable cannot be declaredabstract.
email suggestions to: cs015tas@cs.brown.edu
