Introduction to Object Oriented Syntax in BBj
What is Object Oriented Programming?
If you know Object Orientation from other programming languages, you already know: Object Orientation tries to find a way to combine data with the code that works on the data.
This course does not explain much more than that but rather assumes that you know about Object Orientation from other programming languages. On a side note, you are not forced to use Object Orientation with BBj. Feel free to write programs in the scripted, procedural fashion the language offers.
For a comprehensive tutorial, see the official Custom Objects Tutorial in the BASIS documentation.
Quick Syntax Reference
If you're coming from Java or another OO language, here's how BBj syntax maps to familiar concepts:
Class Definition
class public MyClass extends ParentClass implements MyInterface
field public BBjString Name$
field private BBjNumber count%
method public void sayHello(BBjString name$)
print "Hello, ", name$
methodend
classend
Access Modifiers
| Modifier | Meaning |
|---|---|
public | Accessible from anywhere |
private | Only visible within the same file |
protected | Accessible within the same directory (BBj 21.0+) |
Methods
method public BBjNumber calculateTotal(BBjNumber price, BBjNumber qty%)
methodret price * qty%
methodend
- Use
methodretto return a value (similar toreturn) - Constructors have the same name as the class and no return type
- Add
statickeyword for static methods
Creating Objects
declare MyClass obj!
obj! = new MyClass()
obj!.sayHello("World")
Key Differences from Java
- Block delimiters: Use
classend,methodendinstead of curly braces - Field declarations: Use
fieldkeyword, not just the type - Variable suffixes:
$for strings,%for integers,!for objects - Multiple classes per file: Allowed but discouraged
- Java interop: BBj classes can extend Java classes and implement Java interfaces
General OOP Resources
If you need to refresh your OOP knowledge:
A first Class in BBj
The video shows how to write a first class in BBj. It illustrates the syntax and object oriented code blocks:
After watching it, visit this documentation page for the CLASS Verb in BBj and see another example.
What other verbs would you need for object orientation? Look them up in the documentation.
Reference Classes from Other Programs
To reference other classes, we use the USE verb, like we did in the earlier chapter for referencing a Java type.
In the CarApplication.bbj we use:
use ::Car.bbj::Car
use ::Car.bbj::FlyingCar
to reference the Car and FlyingCar classes in the original file.
An Object Oriented Dialog
Now it's time to write our first graphical object oriented program in BBj. We develop a small dialog to take user input and return it to the calling program:
Watch the video about how it's developed:
In the files section you find a ZIP file which contains the three programs from this chapter. Load the MyDialog.bbj program into your IDE and examine it. Can you improve it?
Additional Documentation and Random Useful Hints
Besides the documentation for the relevant verbs (CLASS, FIELD, INTERFACE, METHOD, EXTENDS, IMPLEMENTS) there is a somewhat dated PDF document that you still might find useful. It's a tutorial that was created when the language received its Object Oriented capabilities.
Some more random hints you might find useful:
- BBj classes can also extend Java classes and implement Java interfaces.
- Since BBj 21.00, BBj classes support reflection. From that version on their bodies get cross-compiled in Java classes for better performance.
- If you are faced with a task to upgrade Object Oriented BBj code from older versions, this document might be a useful read.