BBj Custom Objects Tutorial: Interfaces
Sometimes you will want to define a set of methods that, together, represent a common set of actions. For example, perhaps you find that there are a number of “things” in your system that need to offer the same set of methods related to running: start(), stop(), pause(), and so on. Instead of defining each of those methods in every class that needs them, a simpler approach would be to define them in a single Interface named RunInterface. Then you only need each Class to implement that RunInterface (with each class filling in the specific logic and code details of exactly how it starts, stops, and pauses).
Sharing the RunInterface allows you to ensure that each class offers exactly the same methods to the outside world. It can also make it easier in the future if, for example, you decide that you also need a restart() method on all of those classes—you add it to the RunInterface, and then add the code to each class that implements RunInterface. Sample 2 shows a simple Interface definition, LocationInterface:
Sample 2. A Very Simple Interface
Notice that in an Interface, each method line simply defines how a method will be seen and invoked from the outside (its interface to the world). The code defining how each method works will be provided by each class that implements the Interface.
Using Interfaces is an advanced topic, and we can only touch on it here. See the implements option of the Class Verb help page, and the Interface Verb help page, for more details.
Next Step: Classes
BBj Custom Objects Tutorial Contents
BBj Custom Objects Tutorial: Introduction
BBj Custom Objects Tutorial: Interfaces
BBj Custom Objects Tutorial: Classes
BBj Custom Objects Tutorial: Fields
BBj Custom Objects Tutorial: Methods
BBj Custom Objects Tutorial: Using Custom Objects
BBj Custom Objects Tutorial: Program #1 - Writing a Check
BBj Custom Objects Tutorial: Program #2 - Protected and Private Fields
BBj Custom Objects Tutorial: Program #3: The Static Keyword
BBj Custom Objects Tutorial: Program #4 - Error Handling
BBj Custom Objects Tutorial: Program #5 - Class Inheritance
BBj Custom Objects Tutorial: Program #6 - Callback Choices
BBj Custom Objects Tutorial: Program #7 - Constructors and Field Initialization