HPK taruh disini
Up to now, we have used method specifications for the purpose of promoting data abstraction. That is, a user should focus on the method specifications in a class and ignore the class’s fields and method definitions. Some methods don’t even have a definition, and it turns out that this can be helpful to programmers. For example,
suppose we want to create classes for circles, rectangles and other figures. In each class, there will be methods to draw the figure and to move the figure from one place on the screen to another place on the screen. The Circle class, for example, will have a draw method and a move method based on the centre of the circle and its radius. Here are two method specifications and related constant identifiers that will apply to all of the figure classes:
Each different type of figure will have to provide its own definitions for the draw and move methods. But by requiring that those definitions adhere to the above specifications, we introduce a consistency to any application that uses the figure classes. A user of one of those classes knows the exact format for the draw and move methods—and that will still be true for classes corresponding to new figure-types.
Java provides a way to enforce this consistency: the interface. Each method heading is followed by a semicolon instead of a definition. Such a method is called an abstract method . An interface is a collection of abstract methods and constants. There are no defined methods and no fields.
Kode Iklan 300x250
suppose we want to create classes for circles, rectangles and other figures. In each class, there will be methods to draw the figure and to move the figure from one place on the screen to another place on the screen. The Circle class, for example, will have a draw method and a move method based on the centre of the circle and its radius. Here are two method specifications and related constant identifiers that will apply to all of the figure classes:
final static int MAX_X_COORD = 1024;
final static int MAX_Y_COORD = 768;
/**
* Draws this Figure object centred at the given coordinates.
*
* @param x – the X coordinate of the centre point of where this Figure object will be drawn.
* @param y – the Y coordinate of the centre point of where this Figure object will be drawn.
*
*/
public void draw(int x, int y)
/**
* Moves this Figure object to a position whose centre coordinates are specified.
*
* @param x – the X coordinate of the centre point of where this Figure object will be moved to.
* @param y – the Y coordinate of the centre point of where this Figure object will be moved to.
*
*/
public void move (int x, int y)
Each different type of figure will have to provide its own definitions for the draw and move methods. But by requiring that those definitions adhere to the above specifications, we introduce a consistency to any application that uses the figure classes. A user of one of those classes knows the exact format for the draw and move methods—and that will still be true for classes corresponding to new figure-types.
Java provides a way to enforce this consistency: the interface. Each method heading is followed by a semicolon instead of a definition. Such a method is called an abstract method . An interface is a collection of abstract methods and constants. There are no defined methods and no fields.