Abstract Methods


Abstract Methods

In Chapter 7, we introduced abstract classes as a way to define the framework for a collection of related classes. Among other things, an abstract class can declare abstract methods. An abstract method is designated by the abstract keyword in the method declaration. Abstract methods can only be defined in abstract classes. An abstract method declaration consists of the method name , return type, parameter list, throws clause (if any), and a semicolon. There are no braces and no method body.

Abstract methods are used to impose a certain structure on subclasses of an abstract class. The abstract method provides the basic method syntax to which subclasses must adhere . Nonabstract subclasses must provide an implementation of any abstract methods defined in superclasses. How the methods are implemented is up to the subclass. The only restriction is that the method name, return type, and input parameter list must match the abstract method declaration.

Example: Abstract Methods

To see abstract methods in action, look at the example in the "Abstract Classes" section of Chapter 7 where the Shape class defines two abstract methods intended to compute the area and circumference of a 2-D geometric shape.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net