Object Oriented Programming

“Object Oriented Programming” is a concept in computer science describing how a language can create abstract (or create new ideas of) data structures past just primitive types such as numbers (int, float, etc.) and letters (char). In essence, it describes how you can make your own types, with their own member variables and member functions to describe the idea of this new type.

For instance, say you wanted to describe a “pie” type object (thing) to a computer. This type might have member variables describing flavor, temperature, and diameter, and “method” functions (functions that stay the same in this class) describing actions such as a reheating process that changes its own “temperature” variable. Instead of making seperate variables and functions every time you want to make a “pie” object, this type definition is available to automatically make all the necessary variables and functions.

As a side note, these “type definitions” are often called “classes” or “structs”, but classes and structs leave out primitive types such as “int” and “char”.

The concept of “Object Oriented Programming” also has four “pillars” describing best practices about how these objects should be able to interact with one another:

../_images/pillars.png

Encapsulation

“Encapsulation” means that a class hides its data and inner workings as deemed appropriate, only leaving necessary parts available to the end user. This can reduce mistakes if implemented correctly, but can reduce functionality when done incorrectly.

As an example, lets say someone publishes some code and lots of people start using it. In one of the classes, the developer realizes that using a different data structure would be better. If the class was encapsulated properly, the developer should be able to make this change without any changes to how the class is used, so the people using the code wouldn’t need to change anything.

Abstraction (optional)

This pillar is often left out. Feel free to skip it.

“Abstraction” is similar to “encapsulation”, however instead of hiding implementation details, it doesn’t even include them. In other words, some idea of a function can be given, but the function itself can be empty. This is so different definitions can be given later, as a different form of a class. A class like this is often called an “interface”.

For instance, lets say we had a “vehicle” class. Lets then say we had a “tricycle” class and a “car” class, both sharing data and functions from this “vehicle” class definition. While the “tricycle” and “car” class work very differently, they are both guaranteed to have a “go” function because it was defined in the “vehicle” class they took data from (or implemented).

Inheritance

“Inheritance” describes how “child” and “parent” classes pass data. “Child” classes are normal classes, but they are a copy of some “parent” class, and often expand, or add details to, these parent classes. Not every variable or function will be available to the child class necessarily, as they can be hidden. Keywords that describe what is passed on from a parent class are called “access modifiers”.

For example, lets say there is a “car” class. Lets say there is a “Ferrari” class that inherits from this “car” class. The “Ferrari” class has everything in the “car” class, but has a lot of things added on, such as a V-8 engine and probably a cup holder. However, because the “Ferrari” class’s parent class is the “car” class, it also has things standard in cars such as wheels and axles. It can then be said that the “Ferrari” class inherited these from the “car” class.

Polymorphism

The word “Polymorphism” comes from the greek words “poly” meaning many and “morph” meaning shapes. “Polymorphism”, then, describes how objects can be made to fulfill the function of different types. If an object B inherits from A, that object B can be used in functions that need an object B or an object A.

For example, lets say there is a “street race” function. This function takes two “vehicle” types. Because “tricycle” and “Ferrari” both inherit from a “vehicle” class, they can be used in the “street race” function despite being different types. This is only one example of polymorphism; there are many cases where a variable, function, or class can interact like this.