Intro to Object-Oriented Programming

Published October 16, 2021

Object-Oriented Programming (OOP) is a programming paradigm based on classes as blueprints for objects. An application built with OOP principles is organized into classes. Classes are used to create class instances, called objects.

There are many popular object-oriented programming languages such as JavaScript, TypeScript, PHP, Python, Ruby, C++, and Java.

Classes and objects

A class is a blueprint that is used to create specific objects. We can create an Employee class. This class can have objects for each employee in a company.

Classes usually have functions. They are called methods and they are only available to objects of that specific class type. Methods perform an action on an object. The Employee class might have methods to get the name of an employee, or change the employee's address. These methods will only be used on objects of type Employee. The Employee class can be reused to represent any number of employees.

Classes also have attributes. Attributes store data for each object. The Employee class can have attributes such as firstName, lastName, dateOfBirth, and more.

The value of these attributes can be set for each individual object without affecting other objects of the class Employee. The object that represents employee John Smith can have attribute values that are different from the object that represents employee Jane Doe.

The class Employee is nothing other than a template for modeling an employee in a software application. An object is instantiated from the class to represent one real-world employee, such as John Smith.

The building blocks of OOP

The building blocks of Object-Oriented Programming are:

  • Classes: the blueprints for individual objects. They are data types defined by the programmer.

  • Objects: instances of a specific class created with specific data.

  • Methods: actions that can be performed on objects. Methods can return data for an object or update an object's data.

  • Attributes: the data that describes an object is stored as attributes. Attributes represent the current state of an object.

The principles of OOP

  • Encapsulation: important information is self-contained in an object and only select information is exposed and accessible. It is a data-hiding strategy to achieve greater application security. Learn more about Encapsulation.

  • Abstraction: helps to represent complexity by making only select attributes and methods of an object available. It also helps to generalize one definition of data and to make it applicable to different, but somewhat related, types of data. Learn more about Abstraction.

  • Inheritance: makes code more reusable by having parent classes extend attributes and methods to child classes. Learn more about Inheritance.

  • Polymorphism: allows different types of objects to make use of the same interface, reducing duplicate code. Learn more about Polymorphism.

The benefits of OOP

The benefits of Object-Oriented Programming are:

  • Simplicity: complex real-world entities can be modeled with simple classes.

  • Modularity: the self-contained nature of objects makes the process of collaborative development smoother.

  • Reusability: encapsulation, inheritance, abstraction, and polymorphism make code more reusable.

  • Security: encapsulation and abstraction allow complex code to be hidden.

  • Flexibility: polymorphism allows different objects to share the same interface.