Classes and Objects

Introduction

Xojo is an Object-Oriented Programming (OOP) language. OOP languages are based on Classes and Objects. In this lesson, we’ll cover some of the basics of how classes and objects are built in and used in Xojo.

Problem Description

A bank manages multiple types of accounts: they offer checking accounts, saving accounts and individual retirement accounts to their customers. For all these accounts the IT division has to create programs to deposit money, to withdraw money and to query the account balance. For this simple example a  programmer has to develop three separate programs each with three methods (DepositMoneyWithdrawMoney and GetBalance). It is easy to understand that these methods are identical for the three account types. Only the data item  AccountBalance will differ for the three accounts. A class with its corresponding objects will make it possible to re-use these methods.

What is a Class?

A class is a collection of code (methods) and data (properties) used as a template (a blueprint) for creating objects. The methods and data are called members (member methods, member data) of the class. Referring to our example, the variable AccountBalance is an example of a member data item and DepositMoneyWithdrawMoney and GetBalance are member methods of the class. We need these member methods and the corresponding member data item for all types of accounts. We can give our class (template) the name Account. It is good practice to start the name of a class with a capital letter.

Use title case (all words in the name start with a capital letter) to name a class.
Use title case (all words in the name start with a capital letter) to name a method.

The class definition for our example will have the following structure:

Class Account
   Private AccountBalance As Currency

   Public DepositMoney(amount As Currency)
      ...
   Public WithdrawMoney(amount As Currency)
      ...
   Public GetBalance As Currency
      ...
End Class Account

In the next lesson you will learn how to define this class in Xojo. The following image shows how the Account class will be displayed in the Navigator pane:

Account class

In Xojo a member data item (for instance AccountBalance) is called a Property.

A data item belonging to a class is called a property.
Use title case (all words in the name start with a capital letter) to name a property.

The scope of the property AccountBalance has been defined as Private. A private property can only be accessed from the current class. The methods DepositMoney, WitdrawMoney and GetBalance are declared Public. These methods can be accessed (called) from outside the class.

What is an Object?

An Object is an instance of a class. An object will store and manipulate real data. An object will contain the properties and the methods defined in the class. For our problem, a programmer will have to create three objects:  CheckingAccountSavingsAccount and RetirementAccount based on the class Account. The three objects will all use the same methods (DepositMoneyWithdrawMoney and GetBalance). All objects will have a property called AccountBalance. The value of AccountBalance varies between the three objects. One object (for instance checkingAccount) cannot access the value of AccountBalance of another object (for instance savingsAccount).

In Xojo an instance of a class can be created with the New keyword. The syntax is as follows:

Var reference_variable As New class_name
Examples:

Var CheckingAccount As New Account

Var SavingsAccount As New Account

Var RetirementAccount As New Account

CheckingAccount, SavingsAccount and RetirementAccount are called reference variables. Reference variables will refer to an particular instance (object) of a class. A reference variable is used to access methods and properties defined in the class. To call the method DepositMoney for the object CheckingAccount you write the reference variable followed by a dot (.) and the name of the method or property you want to access. This is called the dot notation.

CheckingAccount.DepositMoney(200.50)

SavingsAccount.AccountBalance = 123.37

Xojo Predefined Classes

Super

There are many predefined classes in Xojo. DesktopLabel, DesktopButton, DesktopTextField… are examples of Xojo predefined classes. By dragging a Label, Button, TextField… on a DesktopWindow you create a new instance (object) of the class. The class on which the newly created object is based, is listed next to the Super property in the Inspector (see red arrow on the screenshot). The object named WelcomeButton is thus an instance of the class DesktopButton. The other elements shown in the Inspector (for instance Name, Left, Top, Height, Width, Caption…) are other properties of this class.

Inheritance

A class that is derived from another class is called a subclass. A user-defined or even a predefined class can be used to create a new subclass. The class from which the subclass is derived is called a superclass . A subclass inherits all public and protected properties and methods from the superclass. Protected properties and methods can be accessed by the superclass and all its subclasses. A subclass can define its own properties, methods and events.

The scope of member properties and member methods in a class can be summarised as follows:

ScopeDescription
PrivateA private property or method can only be accessed from within the class
PublicA public property or method can be accessed from outside the class
ProtectedA protected property or method can be accessed from within the superclass and from all its subclasses
Scope definitions