Introduction
Even though this had been explained over and over by countless people in articles/books/lectures etc., I still see lots of people struggle to define and understand the concept of Object Oriented Programming.
I’ll try my best to explain it to you by using my words and avoid fancy definitions. I suggest you do the same when learning new things: try to explain it in your own words instead of reciting various definitions you find. This will help you better understand the concept and you will also be able to remember it at a later point in time as you don’t have to recall the exact words of the definition to make sense (I personally am very bad at remembering formal definitions, but once I get the concept it’s easier for me to remember it when needed).
Top definitions according to Google
I’ve performed a search on google for the following keywords: what is oop. Here’s the definitions according to the top 5 results from google.
Definition #1: Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.
Definition #2: Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can 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 an object’s procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of “this” or “self”).
Definition #3: The four principles of object-oriented programming are encapsulation,abstraction, inheritance, and polymorphism.
Definition #4: Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure.
In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.
Definition #5: Object-oriented programming (OOP) is a software programming model constructed around objects. This model compartmentalizes data into objects (data fields) and describes object contents and behavior through the declaration of classes (methods).
Even though the above definitions are correct, how easy it is for you to get understand the actual concept just based on this definition?
To better understand the difference, you need to know how programming used to be done back in the day :).
Procedural Programming
There is this thing called Procedural Programming where all programs were written mostly in a single long file (thousands of lines long). It used a top-down approach, all logic of the program would start with line 1 you needed to read and understand each end every line below to understand what was the program doing. In procedural programming, the common mindset was to focus on the steps required to accomplish a goal and that’s about it. Structure of the code was not taken into consideration at first and problems started to show up, especially once the programs started to get more complex/big form a source code perspective.
Let’s say you had to change a value of a variable which was defined in the very beginning of the code. You open the code for the first time and you start reading AND understand 1000+ lines to know where you had to change the code so that the new value was correctly interpreted. It was painful and took forever.
Later, to make the code easier to understand, people started using procedures/routines. Procedures/routines are a set of instructions which aim to perform a task/calculation/etc grouped under a meaningful name (e.g. Compute, GetResults etc). This setup allowed the programmers to call a single procedure more than once instead of copy-pasting the code multiple times when needed (unfortunately such cases still happen today :(), which of course was an improvement. The main problem with it remained the maintenance.
Most people don’t seem to understand that the a project does not end when the development for the app ends and the application is launched for the first time. Usually, the product is created having a specific lifetime (e.g. 1,2,5 years, etc.). In this time, you will most likely find bugs, users will ask for improvements, new features etc. Thus, maintenance to accommodate all these changes is required. The harder it is to understand the code, the harder the maintenance is. In other words, if the code is not very understandable, the fixes/new features will take longer to develop (=more expensive) and will add certain risks. Overall, not a good approach.
Something needed to change, to make things easier.
Object Oriented Programming
And so the Object Oriented Programming model was created. The main purpose of this way of thinking about programs was to make it easier to maintain them.
So, instead only focusing on the steps required to solve a specific problem, let’s also focus on the structure and how to make it as easy as possible to maintain. Thus, the object mindset came to place. Instead of having a block of 50 hard to understand lines of code which accomplish a task, let’s try to think about it as an object responsible with accomplishing that task. Also, let’s change the structure of the code to make it easier to work with, thus the concepts of properties, method, constructor, destructor, field/member came to life. We now have a set of objects with state (properties/fields/members) and behavior(methods) which are responsible to accomplish one or more related tasks.
So when we deal with the next problem, instead of having 1000+ lines of code in a file representing the steps of required to solve a task, we can have several objects responsible for various tasks which work together to solve the issue. Don’t get me wrong, you’re most likely to still have 1000+ lines of code, but the difference is now that these lines of code are split between several objects with very clear responsibilities. This makes each object easier to understand, follow and maintain.
OOP has several principles which aim to help you write more maintainable code. These principles are: abstraction, encapsulation, inheritance and polymorphism. More on these in later articles.
Summary
So, now that I know these, if I were to summarize OOP with my own words I would do it as follows: OOP is a software programming model which aims to increase the maintainability of the code. This is done by using objects (with state and behavior) with a specific responsibility to collaborate in order to solve a given problem. The maintainability property of OOP is further enforced by the four principles: abstraction, encapsulation, inheritance and polymorphism.
It’s not about the syntax, not about the classes, not about the data, it’s about the messaging between objects to resolve a given problem. It’s not me who says it, it’s Alan Kay, inventor of the Object-Oriented Programming concept, see http://wiki.c2.com/?AlanKayOnObjects.
Example
Problem: you have an existing chair which needs to me moved from position X to position Y.
Procedural style: you have to deal with all the details of the chair: what’s it made of, where do the screws go, how many screws required for each leg/back rest etc and finally the position of the chair X. You are interested only in changing this position from X to Y.
Procedural Programming
Object Oriented Style: you have the chair object. You change position from X to Y. That simple. Note: the chair object still has the assembly information in it (what’s it made of, where do the screws go etc.) – probably in the constructor of the object. But you are not interested in these details now. You have an object Chair with a property Position, which value you change from X to Y. chair.Position = Y.
Object Oriented Programming
Have questions? See Facebook or drop me a line.
Comments