BBj Custom Objects Tutorial: Introduction

Starting with BBj® version 6.0, the BBj language offered the ability to define and use custom objects and classes. These BBj custom objects (hereafter referred to solely as “custom objects”) help BBj programmers write object-oriented code that is easier to understand and maintain. For example, the following BBx® program writes "Hello, World!" to the console by defining and using a public class named Speaker:

Sample 1. A Very Simple Custom Object Class

This tutorial will demonstrate how to write custom object classes like Speaker to support object-oriented code development.

What is Object-Oriented Development?

The concept of code being “object oriented” refers to a number of design concepts that together can make code more robust and easier to maintain. Several of these concepts are closely associated with the use of classes in code.

Deciding which fields and methods your code needs in any class is known as Object-Oriented Analysis and Object-Oriented Design (OOA/OOD). That process is outside of the scope of this tutorial, so we won’t spend any significant time explaining it. It is sufficient for you to know that there are numerous online explanations of OOA/OOD concepts, and most of those concepts will apply to BBj, whether they mention BBj or not. The principles of OOA/OOD (how to design classes) are similar for all object-oriented languages, including Java, BBj, C++, C#, and so on.

BBj’s Object-Oriented Constructs

BBj supports object-oriented software development through custom object classes.

Definition:

A custom object class defines a collection of fields (data) and methods (operations) that a developer wants to treat as a single set when writing BBj code. For example, a complete Book class might contain fields for the author’s name, the title, and any number of other values that together would describe “a book”.

Once a custom object class is defined, custom objects can be instantiated and used in BBj code in the same way that Java objects can be used in Java code.

Definition:

A custom object is a specific instance of a custom object class, along with any values that have been assigned to its fields.

In the Book class example above, one instance might have the author name “Leo Tolstoy”, the title “War and Peace”, and so on, while a second custom object of the same class might represent “Mark Twain”/“Huckleberry Finn”, and so on.

Definition:

An interface defines a set of method signatures. Interfaces allow you to share or use the same method definitions in multiple places in your code without having to explicitly define them in each place.

An interface cannot be instantiated because it only defines the method signatures; it does not include code to implement those methods. A class can implement more than one interface.

BBj’s Object-Oriented Syntax

This tutorial will discuss several BBj examples without dwelling on the detailed syntax of each statement. For more details on defining a class in BBj, refer to the online documentation for the class, classend, field, interface, interfaceend, method, methodret, and methodend verbs.

This first section provides some general information about custom object classes and custom objects. The remaining sections of this tutorial provide samples that demonstrate how custom objects can be used in BBj code.

A custom object class is defined by the code between the opening class verb and the closing classend verb. This class definition can be contained within a file that also contains other BBj program code. And, unlike Java code, multiple class definitions can be placed in one BBj source file. However, this is considered poor practice, and should be avoided when possible. As the interpreter moves through a BBj source file that contains a custom class definition, it skips over any custom class definition (class through classend) until it needs that definition later. It does this in much the same way that it skips over the definition of a user-defined function (the code between def fn and fnend).

Next Step: Interfaces

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