Skip to main content

Classes and Objects

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that focuses on the use of "objects" as basic programming units. An object is an instance of a class, which is a template or mold for creating objects.

An object has two main parts: properties (also known as instance variables) and methods (also known as instance functions). Properties represent the characteristics of an object, while methods represent the actions an object can perform.

OOP is based on three fundamental principles: encapsulation, inheritance, and polymorphism. Encapsulation refers to hiding the implementation details of an object, allowing objects to interact with each other through a public interface. Inheritance allows a class to inherit the characteristics and behaviors of another class, while polymorphism allows an object to take different forms depending on the context.

In JARU, you can create classes using the keyword class followed by the class name. Within the class, you can define properties (variables) and methods (functions) that are specific to that class.

Class Declaration

In JARU, classes are a fundamental tool for object-oriented programming. A class is a mold or template for creating objects that share specific characteristics and behaviors.

Class declaration in JARU starts with the keyword class followed by the class name:

class Person
// properties and methods go here
end

In this case, we just created a class called "Person". Within this class we can declare properties and methods.

Property Declaration

In JARU, properties are values associated with a class instance. These properties can be created at runtime and can be accessed and modified both from within the class (using the keyword this) and from outside the class with the syntax myClass.property.

this

The keyword this in JARU refers to the current instance of an object in a class. It's used to access the class's properties and methods from within the class itself.

Property declaration in JARU is done dynamically, meaning it's not necessary to declare them previously in the class. The init method is a common way to initialize properties when creating a new instance of a class.

class Person
def init(name)
this.name = name
end
end

var p1 = Person("John")
var p2 = Person("Mary")

print(p1.name) // shows "John"
print(p2.name) // shows "Mary"

Properties can also be created and modified from outside the class:

class Person
def init()
end
end

var john = Person()
john.alias = "Krillin" // Creates the "alias" property in the "john" object
print(john.alias)
info

Properties are associated with an object instance, not the class itself. This means that each time a new instance of a class is created, an independent copy of its properties is created associated with the instance.

Method Declaration

Methods are functions defined within a class and have access to the class's properties. The syntax for declaring a method in JARU is the keyword def followed by the method name, parameters in parentheses, and must end with end.

class Person
def greet()
print("Hello!")
end
end

Methods can also have parameters and return values:

class Calculator
def add(a, b)
return a + b
end
end

Methods can access a class's properties using this:

class Person
def init()
this.name = ""
end

def greet()
print("Hello! " + this.name)
end
end

Constructors

The constructor is a special method called init() that's used to initialize a class object at the time of its creation:

class ClassName
def init()
// initialization code
end
end
info

The constructor is called automatically when a new object of a class is created. The constructor name must always be init and must be inside the class definition.

class Person
def init()
this.name = "No name"
this.age = 0
end
end

It's also possible to pass parameters to the constructor:

class Person
def init(name, age)
this.name = name
this.age = age
end
end

Constructor Overloading

In JARU, overloading of the init constructor method is allowed, which refers to the ability to have multiple versions of this method with different arguments:

class Person
def init(name, age)
this.name = name
this.age = age
end

def init(name)
this.name = name
this.age = 0
end
end

var p1 = Person("John", 25)
var p2 = Person("Mary")

Overloading the init method is a very useful technique for creating objects in different ways and adapting to the specific needs of the program.

Class Inheritance

Inheritance in object-oriented programming is a mechanism that allows a class (called child class or subclass) to inherit the properties and methods of another class (called parent class or superclass).

In JARU, the concept of inheritance is implemented using the colon ::

class Parent
def init()
property1 = "Value"
end

def method1()
print("Hello, I am method 1 of parent")
end
end

class Child : Parent
def method2()
print("Hello, I am method 2 of child")
end
end

In this example, the Child class inherits from the Parent class, so it has access to "property1" and "method1", and can also have its own properties and methods.

Encapsulation

Encapsulation is a fundamental principle of object-oriented programming that refers to hiding implementation details of a class. In JARU, the private access modifier is used to declare properties and methods that can only be accessed from within the class.

class Person
private age

def init(age)
this.age = age
end
end

var p = Person(25)
print(p.age) // Error, the age property is private
info

In JARU there's no protected clause, but you can access private properties and methods from a child class through inheritance.

Polymorphism

Polymorphism is one of the pillars of object-oriented programming that allows an object of one class to be treated as if it were from another related class. This means that the same action can have different behaviors in different objects, depending on their class.

For example, imagine you have a class called "Animal" and two subclasses "Cat" and "Dog". The "Animal" class has a method called "makeSound" that is overridden in the "Cat" and "Dog" subclasses to emit a different sound. When objects of each subclass are created and makeSound is called on them, each object emits a different sound, but they can still be treated as if they were objects of the "Animal" class.

In summary, polymorphism allows using the same action or method with different objects, and depending on their class, a different action is performed.