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:

  1. Object Declaration – Defining a reference to an object.
  2. Object Initialization – Creating an instance of the object.
  3. 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:

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.