OOP Paradigm
Object-Oriented Programming (OOP) describes programs as collections of mutually interactive objects, each storing user-defined attributes (data or fields) and methods to manipulate them. The set of public attributes and methods forms an object’s Application Programming Interface (API).
OOP follows a three-step process:
- Object Declaration – Defining a reference to an object.
- Object Initialization – Creating an instance of the object.
- Object Usage – Accessing the object’s properties and methods.
Consider the following p5.js example, taken as is from the DOM chapter:
(use the color picker to select the background color)
code
// 1. Object declaration
let colorPicker;
function setup() {
createCanvas(200, 200);
// 2. Object initialization
colorPicker = createColorPicker('#ff0000');
// 3. Object usage
colorPicker.position(10, 10);
}
function draw() {
// 3. Object usage
background(colorPicker.color());
}
Object initialization can be performed in multiple ways:
- Using object literals to define objects inline.
- Calling a p5 method, as seen in the example above or when creating a quadrille object.
- Using a class constructor to create objects with predefined properties and behaviors.
Regardless of how an object is initialized, interaction with its attributes and methods always follows the dot syntax, ensuring a clear and consistent structure in OOP-based programs.