An Introduction To Programming With Processing

Object Oriented Programming

A graphical representation of how OOP can contrast Procedural Programming

Object Oriented Programming Visualzation 01
Object Oriented Programming is a modern day programming paradigm, meaning that it is a fundamental style that suits the task of creating modern software. Most popular modern programming languages support OOP (Object Oriented Programming), and as a result understanding the fundamental concepts that define it within Processing can help in implementing it, by means of adapting your knowledge to suite any programming language that supports Object Oriented Programming. Earlier we discussed that OOP can be contrasted with Procedural Programming if you consider that Procedural Programming is a style of programming where the program is tailored to suite the data as opposed to Object Oriented Programming which is more akin to a style of programming where the data is tailored to suite the program. To briefly recap on these concepts, typically when creating programs using the procedural programming paradigm we use the programming language's built-in API features and associate the data we are representing in our program with the features that are defined by the developers of the language. The data that is associated with the API features is what we then use in our main program. This is in contrast to the object oriented programming paradigm where we define new types of data (called classes) to categorize and associate the data we are representing in our programs with the programming languages built-in API features. We then use instantiations of the classes called software objects in our main program. From this description you can see that the results of both programming paradigms can eventually lead to the same thing, however the process of getting to that result is what distinguishes one programming paradigm from that of another.

OOP as you are aware relies on classes, from which we instantiate objects. These objects are the reason why we refer to this programming paradigm as Object Oriented Programming, and emphasize the “Object” part. We have already been using classes that are a part of Processing's API such as PImage from which we have instantiated object variables which we have given names, and those objects have inherited various properties and functions (called methods in OOP) from the classes from which they where instantiated. But what exactly is a class?

The concept of a class

OOP Branching

A class is simply a body of code that, in a similar way to a function, exists independently of the main body of code from which it is referenced. However, a class does not only have a singular purpose like a function that checks the position of the mouse, or moves and image around in a specific way or has some other purpose that can be summarized by a singularly specific directive. A class can consist of many functions, which in the context of OOP we refer to as methods. These methods can be used like functions of your main program but with the inherited properties of the class from which it came. As a result you can think of a method as being a function of a class, that when used in your main program will, like a function, have a definition independent of the main program and also have properties that are specific to the class it was instantiated from. The benefit of this is that when multiple objects are instantiated from a single class they all inherit the methods of that class, this allows you to use certain methods with one object and certain other methods with another object. Thereby creating relationships between those objects (and ultimately the data you are representing with you main program) with other data or API features in ways that would be very difficult or maybe not practically possible without OOP.