Ring/Lessons/Object Oriented Programming (OOP)

Object Oriented Programming (OOP)
In this chapter we are going to learn how to use the Object-Oriented programming paradigm inside the Ring programming language.

We will learn about


 * Classes and Objects
 * Access Objects Using Braces
 * Composition
 * Setter and Getter
 * Private Attributes and Methods
 * Operator Overloading
 * Inheritance
 * Dynamic Attributes
 * Packages

Classes and Objects
We can define new classes using the next syntax

Syntax:

And we can create objects using the next syntax

Syntax:

Example:

.. note:: We can use { } to access object data and methods.

.. tip:: we can declare the class attributes directly after the class name.

Output:

We can rewrite the same program in another style

Class Point 			# define the Point class x y z 			# the class contains three attributes x, y & z		func print 		# define the print method see x + nl +   # print the x attribute y + nl +   # print the y attribute z + nl 	# print the z attribute

Also we can write the same program in another way

.. note:: we can use the dot operator after the object name to access object members.

Also we can write the same program in another way

.. note:: we can set the default values for the class attributes when we declare them.

Also we can write the same program in another way

.. note:: we can call the init method directly using when we create new objects

Also we can write the same program in another way

.. tip:: using Hash for passing method parameters enable us to create optional parameters and change the order of parameters when adding them to the Hash.

Access Objects Using Braces
We can access the object at any time using braces { }

Inside the braces we can use the object attributes and methods directly

This can be done when we create the object using the New keyword or at any time using the next syntax

Example:

We can use braces to access objects when we call functions or methods

Example:

We can mix between using braces and the dot operator to access the object in the same expression.

Example:

Composition
The object may contains other objects as attributes.

Using braces to access objects can be nested.

Example:

Output:

Setter and Getter
We can define methods to be used when we set and get object attributes.

Syntax:

Example:

Output:

Private Attributes and Methods
We can define private attributes and methods after the keyword private inside the class body

Example:

Output:

Operator Overloading
We can add the operator method to our class to enable using operators with the class objects.

Syntax:

The function operator takes two paramters, the first represent the operator and the second represent the second parameter after the operator.

Example:

Output:

Inheritance
We can create class from another class in the class definition using the keyword from.

Syntax:

We can call a method in the parent class from the child class using the super object.

Syntax:

Example:

Output:

Dynamic Attributes
We can write instructions after the class name to be executed when we create new objects

Example:

.. tip:: in the previous example var1, var2, ..., var10 will be defined as attributes.

.. tip:: The problem with the previous example is that x and cStr will be defined as attributes too!

.. note:: we can write class definitions inside a string then using eval we can execute the string to define the classes

Packages
We can create a package (a group of classes under a common name) using the next syntax

Example

.. note:: we can use the dot operator as part of the package name

Instead of typing the long name PackageName.ClassName we can use the import command

When we import a package, we can use any class inside this package directly.

Example

Printing Objects
We can print the object state (attributes and values) using the see command.

Example:

Output: