BBj Custom Objects Tutorial: Program #2 - Protected and Private Fields
Example Programs
Here are some example BBj programs. Each example is designed to demonstrate some aspect of using Custom Object Classes in BBj.
The complete source code for each program is included in an appropriately named sub-folder in the zip file you can download here.
Program #2: Protected and Private Fields
Let’s take a look at a slightly more complex BBj program that uses a superclass, Check, and a subclass, PayrollCheck. Once we have defined these classes, we will run code that creates an instance of a PayrollCheck class and then assigns that instance to a variable with a declared type of the Check class. Once we have done this, we use the Check class’s methods to process the PayrollCheck as a Check. After all, since it is a subclass of the Check class, a PayrollCheck is “a type of Check”. Here is the code, with each of the three source files shown using a different border color for readability:
Program 2. Writing Out a PayrollCheck
As the developer, we need to define the Check class, which we do in the Check.src file (with the green border above).
We also need to define a PayrollCheck class, which is a subclass of the Check class. We do that in the PayrollCheck.src file (with the blue border above).
Finally, we need to write the code to use both classes. We do that in the WriteAPayrollCheck.src file (in the red border above).
We defined both classes and the code to use them, each in their own BBj source file, so that each is available to use independently or together, and so that changes to one file will not accidentally affect the other.
Execution
Creating the three BBj source files and then running Program 2 (the code in WriteAPayrollCheck.src) resulted in the following SysConsole output:
Figure 2. The Output from Program 2
Observations
There are some details worth pointing out about this code.
The variable paycheck! is declared as an instance of the Check class, but it is assigned a value that is a PayrollCheck. This is a valid assignment since PayrollCheck is a subclass of the Check class.
Since paycheck! is declared as an instance of the Check class, only the methods available for a Check can be called on the variable paycheck!.
In WriteAPayrollCheck.src, we see these lines that demonstrate BBj’s USE Verb:
USE ::Check.src::Check
USE ::PayrollCheck.src::PayrollCheck
USE java.lang.Integer
Program 2-1. BBj’s USE Verb
Each USE statement tells BBj where to find the definition of a class that is defined elsewhere. That class can be either a BBj Custom Object class defined in another BBj Source file (as with the Check and PayrollCheck USE statements above), or a Java class defined by the installed Java Development Kit (JDK) (as with the java.lang.Integer USE statement above). As an added convenience, once BBj is able to find a class specified with a USE verb, BBj code can refer to that class by just its name, without having to specify a filename (BBj) or package name (Java). The USE statements permit us to use a class name as a type in a DECLARE statement like these BBj and Java examples:
declare Check paycheck!
declare Integer count!
Program 2-2. Using Just the Class Names as a Type in a DECLARE Statement
As opposed to fully qualifying the type each time it is used, like this:
declare ::Check.src::Check paycheck!
declare java.lang.Integer count!
Program 2-3. Using Fully Qualified Types in DECLARE Statements
Fully qualifying the type is always valid, but it can become tedious after a while.
Next Step: Example Program #3
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