Skip to content

OOP and Cali Lang

Austin edited this page May 4, 2017 · 1 revision

Cali-Lang is based upon Java and like Java cali is an Object Orient Programming Language (OOP). This means that all variables (members) and functions are defined within an object. Cali natively supports basic OOP features such as inheritance and polymorphism. This means that it is easy and natural to create organized and extensible code.

Since cali is an OOP language, we cannot just start out writing expressions and function calls, we must first create a class. A class represents what will be an instance of your object. To run a class, we must define a function as the "entry point" to our application. This means that we need to have a way of telling cali which function to start executing first. This function is the main function. Each class may only have one main function and when you execute a .ca file from the command line cali will look for this main function to execute.

We'll use our helloworld.ca application as an example and go line by line through the code.

The include statement below tells cali to include the sys.ca module, which is a cali standard library module.

include sys;

This is the class definition. It creates a new class of type 'helloWorld'. Within the class definition we can create member variables and functions. Any member or function within the class can be referenced using the 'this' key word. More on members and 'this' key word later.

class helloWorld {
	// ...
}

The main function is the entry point for the application. Main is the first function that cali will run when executing a .ca file. Notice that the main function has a single argument called args. The args variable is a list of strings that are any command line arguments to pass to the application. The first line within the main function stores the "Hello World" string into the local msg variable. The second line is a function call 'println' to the sys object with the msg string defined above. The sys.println(msg) call is what prints the message to the command line.

One thing to note is that when we store "Hello World" into msg variable, msg is considered a local variable and only exists within the main function. Other functions cannot access msg variable and it can be cleaned up (deleted) as soon as the main function has completed.

public main(args) {
	msg = "Hello World";
	c.log(msg);
}

For demonstration purposes, we will add to our helloworld.ca example. We will add a member variable, a function, and a constructor.

A constructor is a function that is called when an instance of a class is created. This is also called instantiation, or instantiating the object. In this example we instantiate a new helloWorld object within the main function using the 'new' key word. This tells cali to create a new instance of helloWorld and assign it to the world local variable.

Within the constructor function, we make a call to the setName function with a string argument of "Avery". The setName function then takes the passed string of "Avery" and assigns it to the member variable 'name' which is defined toward the top of the helloWorld class.

include sys;

class helloWorld {
	// Member variables.
	public name = "";
	
	// Constructor
	public helloWorld() {
		this.setName("Avery");
	}
	
	public main(args) {
		// Create new helloWorld instance
		// which calls the constructor.
		world = new helloWorld();

		c.log("Hello " + world.name);
	}
	
	public setName(Name) {
		this.name = Name;
	}
}