# Object-Oriented Programming

## Procedural and Object-oriented Programming

Procedural programming is a method of writing software. It centers on the actions that take place in a program - most early programming languages were procedural. Procedures (functions) operate on data items that are separate from the procedures.

Object-oriented programming is centered on objects. Objects are created from abstract data types that encapsulate data and functions together. An OBJECT is a software entty that contains both data and procedures wrapped together. Data that is contained in an object is known as the object's DATA ATTRIBUTES. Attributes are simply variables that reference data. The procedures that an object performs are known as METHODS. Methods are functions that perform operations on the object's data attributes. The object is a self-contained unit consisting of data attributes and methods.

Thus, code and data is not separated - this is called ENCAPSULATION. Data hiding refers to an object's ability to hide its data attributes from code that is outside the object. Only the object's methods may directly access and make changes to the object's data attributes. An object typically hides its data, but allows outside code to access its methods.

An object is not a stand alone program, but is used by programs that need its services.

## Advantages of Object-Oriented Programming

- Modularity for easier troubleshooting. Objects are self-contained with both data and function (encapsulation). So when a car object broke down, the problem must be in the car class.
- Reuse of code through inheritance. Because the child classes inherit attributes and methods from their parent class, you do not need to rewrite the same functionalities for all the child classes.
- Flexibility through polymorphism. You are allowed to shape-shift or adapt the same method that the child class inherited from their parent with different functionalities.
- Effecitve problem solving. Object-oriented programming is often used to associate real-world objects and processes with their digital counterparts, thus, it is often the most natural and pragmatic approach for solving a problem.

## OOP Concepts

### Class

A class is code that specifies the data attributes and methods for a particular type of object. It is a "blueprint", or a description of a group of objects with similar attributes. For example, a Person class describes the shared attributes and methods of people.

### Object

An object is an instance of a class. Take the Person class as an example, an instance of Person will be "John", who is an OBJECT of the type person. Notice the difference between class and object: the Person class is just a definition, whereas the object "John" is a "real" person. You can create multiple objects using the same "blueprint" - the Person CLASS. So, in addition to "John", we can also create "Joe", "Jane", and "Jeff".

### Encapsulation

Combining data (attributes) and actions (methods) into the same block of code called a class.

### Inheritance

Allowing one object to acquire the properties and behaviors of the parent object by creating a parent-child relationship between two classes.

### Polymorphism

Allowing the parent class and its children classes to have the same function (method) with different behaviors. For instance, both Circle and Square classes are derived from the Shape class. However, the draw() method in Circle and Square can behave differently.

### Composition

Objects can contain other objects in theie instance variables - attributes. For instance, a Person, "John", can own a "Honda Civic", which is an instance of the Car class.

### Abstraction

Abstract classes are cosnidered as a blueprint for other classes. They cannot be instantiated - meaning that you cannot create objects using the abstract classes.

## Techniques for Designing Classes

To help in identifying the classes you need for an application, you will need to:

1. define the problem domain (what is it you need to solve)
2. identify all nouns in the description - these will be a potential class
3. refine list to those nouns (classes) relevant to the problem
4. consider what must a class know - or what data do we need to support this class? These are the attributes of the class.
5. consider what responsibilities does a class have? 1) what are the things that the class is responsible for knowing; 2) what are the actions that the class is responsible for doing? These are your Methods.

When designing a class it is helpful to draw a UML diagram - Unified Modeling Language. A UML Diagram uses the following format to represent a class:

## Further Reading

- How to Explain OOP Concepts
- UML Class Diagram Tutorial
- w3schools.com - Python Classes