What Is Object-Oriented Programming?

Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.

class is an abstract blueprint used to create more specific, concrete objects. Classes often represent broad categories, like Car or Dog that share attributes. These classes define what attributes an instance of this type will have, like color, but not the value of those attributes for a specific object.

Classes can also contain functions, called methods available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific type of object.

For example, our Car class may have a method repaint that changes the color attribute of our car. This function is only helpful to objects of type Car, so we declare it within the Car class thus making it a method.

Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like myCar or goldenRetriever. Each object can have unique values to the properties defined in the class.

For example, say we created a class, Car, to contain all the properties a car must have, colorbrand, and model. We then create an instance of a Car type object, myCar to represent my specific car.

We could then set the value of the properties defined in the class to describe my car, without affecting other objects or the class template.

We can then reuse this class to represent any number of cars.

Building blocks of OOP

Next, we’ll take a deeper look at each of the fundamental building blocks of an OOP program used above:

  • Classes
  • Objects
  • Methods
  • Attributes

Classes

In a nutshell, classes are essentially user defined data types. Classes are where we create a blueprint for the structure of methods and attributes. Individual objects are instantiated, or created from this blueprint.

Classes contain fields for attributes, and methods for behaviors. In our Dog class example, attributes include name & birthday, while methods include bark() and updateAttendance().

Here’s a code snippet demonstrating how to program a Dog class using the JavaScript language.

Remember the class is a template for modelling a dog, and an object is instantiated from the class representing an individual real-world thing.

 

Objects

Of course, OOP includes objects! Objects are instances of classes created with specific data, for example in the code snippet below Rufus is an instance of the Dog class.

When the new class Dog is called:

  • A new object is created named rufus
  • The constructor runs name & birthday arguments, and assigns values

Programming vocabulary:

In JavaScript objects are a type of variable. This may cause confusion, because objects can also be declared without a class template in JavaScript, as shown at the beginning.

Objects have states and behaviors. State is defined by data: things like names, birthday, and other information you’d want to store about a dog. Behaviors are methods, the object can undertake.

N/A What is it? Information Contained Actions Example
Classes Blueprint Attributes Behaviors defined through methods Dog Template
Objects Instance State, Data Methods Rufus, Fluffy

Attributes

Attributes are the information that is stored. Attributes are defined in the Class template. When objects are instantiated individual objects contain data stored in the Attributes field.

The state of an object is defined by the data in the object’s attributes fields. For example, a puppy and a dog might be treated differently at pet camp. The birthday could define the state of an object, and allow the software to handle dogs of different ages differently.

 

Methods

Methods represent behaviors. Methods perform actions; methods might return information about an object, or update an object’s data. The method’s code is defined in the class definition.

When individual objects are instantiated, these objects can call the methods defined in the class. In the code snippet below, the bark method is defined in Dog class, and the bark() method is called on the Rufus object.

Methods often modify, update or delete data. Methods don’t have to update data though. For example the bark() method doesn’t update any data because barking doesn’t modify any of the attributes of the Dog class: name or birthday.

The updateAttendance() method adds a day the Dog attended the pet sitting camp. The attendance attribute is important to keep track of for billing Owners at the end of the month.

Methods are how programmers promote reusability, and keep functionality encapsulated inside an object. More on encapsulation and data abstraction in the Principles of OOP section. This reusability is a great benefit when debugging. If there’s an error, there’s only one place to find it and fix it instead of many.

The underscore in _attendance denotes that the variable is protected, and shouldn’t be modified directly. The updateAttendance() method is used to change _attendance.