## Procedural Programming versus Object-Oriented Programming

It is important to understand how object-oriented programs differ from procedural programs. The main goal of object-oriented programming is to allow the problem being solved to dictate the class design. For instance, a house is organized as a function of its rooms: living room, kitchen, bedroom, and so on. Each room is organized as a function of its furniture and its use. Each room could define a new class containing attributes that define the furniture and methods that define their use. The process of breaking a problem down into a set of classes each with its own set of attributes and methods is called data abstraction. This process is in stark contrast to procedural programming. Read this page to see how these approaches differ.

### Objects and Classes

#### Overview

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). There is significant diversity of OOP languages, but the most popular ones are class-based, meaning that objects are instances of classes, which typically also determine their type.

#### Discussion

Thus far, we have focused on procedural programming. Based on structured programming, procedures (routines, subroutines, or functions) contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program's execution, including by other procedures or itself. The focus of procedural programming is to break down a programming task into a collection of variables, data structures, and subroutines. Small programs and scripts tend to be easier to develop using a simple procedural approach.

Object-oriented programming instead breaks down a programming task into objects that expose behavior (methods) and data (members or attributes) using interfaces. The most important distinction is that while procedural programming uses procedures to operate on separate data structures, object-oriented programming bundles the two together, so an "object", which is an instance of a class, operates on its "own" data structure. Larger programs benefit from better code and data isolation and reuse provided by an object-oriented approach.

Objects and classes are often designed to represent real-world objects. Consider a door as an example of a real-world object. Most doors have limited functionality. They may be opened and closed, and locked and unlocked. In procedural programming, we might design functions to open, close, lock, and unlock a door, such as:

Procedural Programming - Functions
OpenDoor(door)
CloseDoor(door)
LockDoor(door)
UnlockDoor(door)

Object-oriented programming combines code and data, so that, rather than having separate functions act on doors, we design doors that have methods that can act on themselves. Methods represent something the object can do, and are typically defined using verbs. Object-oriented door pseudocode might look like:

Object-Oriented Programming - Methods
door.Open()
door.Close()
door.Lock()
door.Unlock()

Objects may also have attributes, something the object is or has, and are typically defined using nouns or adjectives. Door attributes might include:

Object-Oriented Programming - Attributes
door.Height
door.Width
door.Color
door.Closed
door.Locked

When we write code to define a generic door, we would create a door class. The door class would contain all of the methods a door can perform and all of the attributes a door might have. We would then create instances of the class (objects) to represent specific doors, such as a front door, back door, or room door on a house, or a left door and right door on a car.

#### Key Terms

attribute

A specification that defines a property of an object.

class

An extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

instance

A concrete occurrence of an object.

method

A specification that defines a procedure or behavior of an object.

object

A particular instance of a class where the object can be a combination of variables, functions, and data structures.

this, self, or Me

Keywords used in some computer programming languages to refer to the object, class, or other entity that the currently running code is part of.

Source: Dave Braunschweig, https://press.rebus.community/programmingfundamentals/chapter/objects-and-classes/