WEEK: 5
Active: February 21st - February 27th
Work Due: February 28th @ 8:00AM

OOP Terminology

This page will go into some of the terminology of object oriented programming a bit more.

The first thing to know is the name of the paradigm itself. Again, object oriented programming is a shift from procedural programming in which every line flows to the next, or even structural programming which encapsulates some information or common tasks as things to do after certain events (we started to explore this last week informally). Object oriented programming is a shift towards the encapsulation of ideas, data, and digital things as objects, whose properties and methods are encapsulated away from the main program. After instantiating or creating a new object, this object can then interact with other objects or the program itself.

This will be a shift in thinking for many of you. Away from a line-by-line approach, towards an abstracting of ideas into separate parts of your code. Again, this is a difficult switch, and that is recognized.

Object

An object, in computer code, is a digital thing. In JavaScript, we have seen one, simple way, of how to create objects, using the curly-bracket notation. This is technically known as an object literal.

let myObj = { property1: someValue, property2: someOther Value };

However, starting this week, we will start to create objects using classes. These objects will be the digital representation of some class “blueprint” or “recipe” as the videos alluded to.

An object is often described as an instance of a class.

Class

A class is an abstract representation that encapsulates what it means for something to be “that thing”.

Let me unpack that…

  • A class is set of instructions, comprised of properties and methods.
  • Together, these properties and methods can be used to define a digital object. Whether that is a digital representation of an employee (low-level, assistant, manager, CEO, etc.), or some set of data.
  • The class is not the actual “thing” but rather a set of instructions describing the potential for that “thing”.
  • This is an abstract representation of an object. It is the potential of an object. Or, it is a description of what is necessary for an object to actually exist, along with a set of instructions for how that object would exist in the digital world. Rather, the data and ways of acting on the data are encapsulated together in a single objet.

Encapsulation

Encapsulation is the gathering of data or properties that define an object, along with the methods or functions that are used to enact change on these properties.

Encapsulation is what allows objects to be created from classes. The encapsulation of both properties and methods in a single object allows programmers to treat the object as a self-contained unit.

Abstraction

Abstraction is the reduction of code by a programmer, such that an object can be treated as a “black box”. The programmer who “uses” an object of class type X simply needs to know about its properties and methods NOT how these are implemented. In this way, the class abstracts away or hides the complexity of the object.

Property

An object or class property is a piece of data used to define the resulting object.

For example, if we have two “car objects”, the parent class might have a property that defines color. The color property of the first car may be red and the second blue.

In this way, the properties of classes are used to define the specifics of a resulting object.

NOTE: Class/object properties are also sometimes referred to as;

  • attributes
  • data
  • fields

Method

An object or class method is a function that defines something to do with an object, or some way of processing an objects data/properties.

For example, if we were writing a car class in p5, we may want to add a method that would display the car the web canvas. This method would query the objects properties to determine things like, color, speed, direction, and maybe size. This method would then use these properties along with other information that helps define a car, to draw an image of a car on the canvas.

Instantiation (aka. new)

When we create a new object from a class, we are instantiating the new object.

As we will see throughout this week’s content, to create a new instance of a class, we will use the JavaScript keyword new.

this

A potentially confusing idea is the reference to “specific instances” of an object. When referring to a specific instance of an object, or, when referring to an object’s own instance (from within the object) we will use the term this.

The this keyword in JavaScript is used throughout the language, and can be very confusing (hence why it has remained anonymous until now). Before we dive deep into this, just try and accept that this refers to a specific instance of an object from within that object itself.