OOPs Concepts in Prolix
- Class
- Objects
- Polymorphism
- Encapsulation
Prolix Class
A class is a collection of objects. A class contains the blueprints or prototypes from which objects are created. It is a logical entity that contains a number of attributes.
To comprehend the concept of classes, let's delve into a scenario where you need to manage information about various employees in a company. If you were to use a simple list to represent each employee, you might assign the first element for their name, the second for their department, and the third for their hire date. However, as the number of employees grows, it becomes challenging to discern which element corresponds to which attribute, leading to confusion.
Now, imagine there are 50 employees, each with additional details like job title, salary, and performance rating. Using a list would result in an unwieldy structure, making it difficult to keep track of the information coherently.
This is where classes come into play. Instead of relying on a list, you can create a class named 'Employee' with attributes such as name, department, hire date, job title, salary, and performance rating. Each employee can then be represented as an instance of this class, providing a structured and organized way to manage and access information. Classes offer a clear and intuitive framework for handling complex data, making it easier to extend and maintain as the scope of information grows.
Some points on Prolix class:
- Classes are created by keyword
class
. - Attributes are the variables that belong to a class.
-
Attributes are always public and can be accessed using the
colon (
:
) operator.
Class Definition Syntax:
class ClassName :Attribute1 <value> :AttributeN <value>;
Creating an Empty Class in Prolix
class Employee;
Prolix Objects
In programming, an object is a fundamental concept representing an entity that combines both state and behavior. This concept is not limited to abstract ideas but extends seamlessly to real-world entities such as a mouse, keyboard, chair, table, or pen. These objects encapsulate attributes or properties (state) and actions or methods (behavior) that define their identity and functionality.
Even the most basic data types in programming, like integers and strings, are treated as objects. For instance, the number 12 is an object, and so is the string "Hello, world." This object-oriented perspective allows programmers to apply a unified approach to various elements, making code more organized and intuitive.
Going a step further, more complex data structures like arrays and dictionaries are also treated as objects. Arrays, serving as objects, can contain a collection of other objects, facilitating data organization. Similarly, dictionaries act as objects that establish key-value pair relationships, offering flexibility in representing connections between different elements.
It's worth noting that every instance of a basic data type is, essentially, an object. Each object comes with its own set of attributes and methods, providing a cohesive way to interact with and manipulate data. This object-oriented approach is pervasive in programming and plays a crucial role in building modular, scalable, and maintainable code.
In summary, the concept of objects is ingrained in programming, offering a systematic way to model both simple and complex entities. Whether you're dealing with a single integer, a string, or more intricate data structures, the principles of objects persist, contributing to a coherent and efficient programming paradigm.
An object consists of:
- State: It is represented by the attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object but in Prolix it is usually manipulating attributes and running code. It also reflects an object's response to other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
To understand the state, behavior, and identity let us take the example of the class employee.
- The identity can be considered as the name of the employee.
- State or Attributes would represent the characteristics or information associated with each individual employee.
- Behaviors would represent the actions or operations that an employee can perform or undergo.
Creating an Object
Employee $obj;
Class object reference
In Prolix, class's name is a conventional pre-defined object of instance attributes in a class. It is used to reference the instance (object) on which a attribute is affected. This object is implicitly passed when a attribute is affected on an object, allowing the attribute to interact with the specific instance's attributes.
In Prolix, the programming paradigm centers around a unique concept where objects are defined primarily by their attributes, and there are no explicit methods. The behavior of an object is determined by manipulating its special attributes directly. This attribute-centric approach serves as the driving force for code execution and functionality within the language.
class MyObject :attr 5 :__index__ { $io:write $MyObject:attr; # The output will be "5" if the object has been created };
The Prolix __index__
Attribute
This specific attribute, referred to as "initial_state," undergoes activation upon the instantiation of an object from a class. Designed with precision to encapsulate a particular facet of the object's initial configuration or behavior, this attribute dynamically becomes active and assumes a predetermined state as the object is created, thereby establishing a foundational status for the newly instantiated entity.
class Employee :__index__ { $Employee:name "John"; }; Employee $employee; $io :write "The employee name's " :write $employee1:name :write ".\n"; $employee:name "David" $io :write "Now the employee number name's " :write $employee:name :write ".\n";
Creating objects with affectable attributes
In the context of Prolix, when a class incorporates a special
attribute named __edit__
, a distinctive mechanism
is established. This mechanism is designed to create new
affectable attributes, and when an object is instantiated from
this class, the __edit__
attribute serves as a
callback or handler. Notably, as object attributes undergo
editing, the name of the edited attribute is automatically
stored in another designated attribute named
__key__
. This enables developers to dynamically
track and respond to attribute modifications by referencing
the __key__
attribute, providing a powerful and
extensible tool for managing changes in object state within
the Prolix programming paradigm.
The driver code starts by creating two instances of the
Employee class: John and David. The ask_name attribute is
affected in both instances ($John.ask_name
and
$David.ask_name
), causing each employee to print
a statement with its name.
class Employee :name none :__edit__ { $math:eq $Employee:__key__ "ask_name"; if $math:result { $io:write "My name is " :write $Employee:name :write "\n"; } }; # Driver code Employee $John :name "John"; Employee $David :name "David"; $John:ask_name; $David:ask_name;
Expected output:
My name is John My name is David
Prolix Polymorphism
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different types to be treated as objects of a common type. This concept promotes code reusability, flexibility, and abstraction.
Polymorphism and Object-Oriented Programming
Polymorphism has vast applications in Object-Oriented Programming. Let’s now create a simple class with polymorphic affectable attributes:
class Animal :type "animal" :__edit__ { $math:eq $Animal:__key__ "display_type"; if $math:result { $io:write "This is a " :write $Animal:type :write "\n"; } }; Animal $Dog :type "dog"; $Dog:display_type;
In this example, we create a basic class called
Animal
with a affectable attribute
display_type
that prints the type of the animal.
We then create a new Dog
object from the
Animal
class and modify its type to
Dog
. When the Dog's
display_type
attribute is affected, it now prints
This is a dog
.
Polymorphism with Shared Affectable Attribute Names
Polymorphism allows different objects to share affectable
attribute names. Let’s illustrate this with another example
using our Animal
class:
Animal $Cat :type "cat"; $Cat:__edit__ { $math:eq $Animal:__key__ "display_type"; if $math:result { $io:write "This is a cute " :write $Animal:type :write "\n"; } }; $Dog:display_type; $Cat:display_type;
Expected output:
This is a dog This is a cute cat
Prolix Encapsulation
Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling data (attributes) and affectable attributes (methods) that operate on the data into a single unit known as a class. The primary purpose of encapsulation is to hide the internal implementation details of an object and expose a controlled and well-defined interface for interacting with it.
class Circle :__edit__ { $math:eq $Circle:__key__ "calculate_area"; if $math:result { $math:mul $math:pi $Circle:radius :mul $math:result $Circle:radius; } };