Introduction to OOP concepts

Reading time10 min

In brief

Article summary

In this article, we present the basic concepts related to object-oriented programming (OOP). In particular, we study the concept of classes and objects and how they are related to each other. We then focus on two main aspects of OOP: encapsulation and inheritance.

Main takeaways

  • Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields (often known as attributes or properties), and code in the form of procedures (often known as methods).

  • Object-oriented computer programs are designed as objects that interact with one another.

  • An object is an (software) abstraction of a real-world entity involved in the execution of a program. It is composed of an identity, a state and a behavior.

  • A class is an entity that defines the structure (state or attributes) and behavior (methods) of a family of objects. It consists of a name, attributes, methods, and (a) constructor(s).

  • Encapsulation is the principle of hiding or protecting (part of) the state of an object so that it cannot be directly accessed from outside the class.

  • Inheritance is a way of structuring code that defines hierarchical relationships between classes.

  • Attributes and methods of a parent class automatically become attributes and methods of its subclasses.

  • Subclasses can modify the parent class methods (methods of the parent class can be redefined) and add new ones to better characterize the subclass.

Article contents

As mentioned in the course dedicated to programming paradigms, object-oriented programming (OOP) was introduced to facilitate the creation and maintenance of programs that solve complex problems. The principle is very simple: to group data and actions that manipulate them in the same entity, and limit (or even forbid) direct access to the data. These entities are called objects in OOP. The aim of this course is to introduce the notion of object in OOP and some of the associated concepts.

Definition

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code: data in the form of fields (often known as attributes or properties), and code in the form of procedures (often known as methods). In OOP, computer programs are designed as objects that interact with one another.

1— Objects, attributes, and methods

Unlike procedural programming, a program written in OOP distributes the effort involved in solving a problem over a set of objects that collaborate by sending messages. An object is composed of :

  • an identity,
  • a state, represented by variables called attributes or properties or fields,
  • a behavior, that may modify the state and is implemented by a set of functions called methods.
Definition

An object is an (software) abstraction of a real-world entity involved in the execution of a program.

The figure below illustrates the fundamental difference between the structure of an object-oriented program and a procedural program in the case of a bank account management system. In the procedural version, the program is decomposed into several functions that share common data (in our case transactions, with the use of tuples to store the pairs (user, amount)). In the object-oriented version, the concept of bank account and user are implemented as objects.

A bank account management program using OOP vs using procedural programming A bank account management program using OOP vs using procedural programming Figure 1: Object-oriented vs procedural version of a bank account management program.

Objects interact with each other by sending messages, which in turn can be answered, so that they delegate certain tasks to their collaborators. Objects are said to interact through method calls.

A message:

  • specifies a method (m) of the target object,
  • also contains any arguments (args),
  • is sent by a source object (s) to a target object (t): t.m(args),
  • causes m(args) of object t to be executed,
  • eventually t returns the result from m to s,
  • s can then continue its execution.

Using the example of Figure 1, if the user user_1 wishes to make a deposit of 500 euros on the bank account represented by the object bank_account_1, the code of the user_1 object will contain bank_account_1.deposit(user_1, 500) which execution implies sending the message deposit(user_1, 500) to the object bank_account_1.

2— Classes, instanciation, and constructors

In OOP languages, an object is created from a class. The formal term for object creation is instantiation, objects being also refered as instances of a class. A class is an entity that defines the structure (state or attributes) and behavior (methods) of a family of objects. It consists of :

  • a name,
  • attributes (their name and, if applicable, their type),
  • methods (their name, return type, name and, if applicable, type of each argument, and their code),
  • constructor, a particular method called when an object is created and which role is to initialize the object state (i.e., its attributes).

For example, for the bank account management program, we may have three classes: BankAccount, User, and Address. In Figure 1, there are two objects that are an instance of the BankAccount class, two instances of the User class and two instances of the Address class.

3— Encapsulation

Encapsulation is the principle of hiding or protecting (part of) the state of an object so that it cannot be accessed from outside the class. It can therefore only be directly accessed (and modified) from the object’s methods. This security of an object’s state :

  • Protects the object’s internal data. For example, it prevents an attribute from being modified, or guarantees that an attribute is updated only when another one is modified.
  • Simplifies object utilization. It can only be done by calling the object’s methods, and not by directly manipulating its attributes.
  • Disconnects the use of an object from its internal structure. Since attributes are not directly accessible from the outside of the class, it is possible to modify the implementation of an object’s state without affecting code on other objects. In our example of a bank account management program, switching from a simple list of transactions containing the amount of a deposit or withdrawal operation to a list that also contains timestamps will not affect code of user objects.

As a result, the whole execution process of an object-oriented program is based on a simple principle of responsibility distribution: each object must take care of its own attributes. This programming is fundamentally distributed, modularized, and decentralized.

4— Inheritance

Inheritance is a way of structuring code that defines hierarchical relationships between classes. This very simple concept is inspired by our own cognitive way of conceptualizing the world. For example, a laptop can be described at different levels of precision: a machine, a computer, a notebook, a Lenovo ThinkPad. Similarly, in an object-oriented application, we can create a base class (called parent class) and more specific subclasses (or child classes). As an example, consider the PyRat game program. The Player class represents a general player (its state is just a name and a skin) with no stategy to search for a piece of cheese in the maze. For each graph traversal strategy, you have created a new class as a Player subclass. This technique helps us to organize and reuse code efficiently.

When using inheritance, the code of a subclass may be:

  • attributes and methods characterizing the parent class, as they automatically become attributes and methods of the subclass, without the need for further specification, i.e., attributes and methods from a parent class are inherited by the subclass,
  • new attributs and methods, added to better characterize the subclass,
  • methods from the parent class which behaviour has been modified, i.e., the behaviour of the parent class methods can be overridden in the subclass.

To go further

Important

The content of this section is optional. It contains additional material for you to consolidate your understanding of the current topic.

  • Polymorphism in OOPs.

    Introduction to one of the fundamental principles of OOP, polymorphism, and related concepts, method overloading and overriding.

To go beyond

Important

The content of this section is very optional. We suggest you directions to explore if you wish to go deeper in the current topic.