Sunday 8 March 2015

Doodad Directed Programming (An overview on OOP)

I feel humbled by my friend Dima's explanation of Object Oriented Programming (OOP). It is a very entertaining account that includes both technical terms and his personal experience on learning about and applying this concept. Having said that, I will do my best to explain what OOP is and why it is important.

What is Object Oriented Programming (OOP)?
Some friends of mine believe that programming involves a lot of binary digits and their bending à la Matrix. What seems to be a lesser known fact about programming is that there are different levels of communication between programmers and machines.

Diagram 1: Representation of programmer / machine levels of communication

As you can see in Diagram 1, we have hardware at the bottom and programming languages at the top. Machine language is the only language that machines understand and it is composed mainly of numbers, and each CPU uses a different language, hence it is impractical for a person to learn it. To make it practical for people to create programs, high-level languages were created. These resemble English (i.e. if you don't know English, you might have a harder time understanding why things are the way they are in programming), but some more than others.

OK, this explanation seems to have drifted away from OOP. Do I still have a point to make here? Yes, and it has to do with the mentality applied when creating high-level languages. Machines do not know what a number is, or the heavy burden of daily life. They know about numbers (probably in the form of binary gates XOR XOR 110011101) and that's it. Hence, when creating a language they needed to tell the machine what a number is and how it works. They also needed to tell the machine what a string is and how it works. And so on...

But why should the person who engineers a programming language carry the burden of describing everything in the whole wide world. We find objects in the world, and they have definitions and rules. OOP is centered around creating objects, properly defined through attributes and following rules through methods, and them being instrumental when fulfilling a goal.

In short, OOP revolves around creating and using objects (called classes in Python and other programming languages), which are data structures that have attributes and methods.

What is a class?
A class is a data structure that has both attributes and methods (you are probably tired of listening these two words). An attribute is any form of data that is contained inside the class. A method is a function that is part of the class, and typically focuses on working with the attributes that are part of the class.

Classes can have children. Why couldn't they? As long as they are caring parents and pass on some attributes and methods, there should not be a problem.

What is inheritance?
Inheritance occurs when a subclass is initiated. This subclass will have all the attributes and methods of the parent class, unless they are overridden in the subclass itself. The idea is that you can make a main, generic class and have its offspring be specific for a certain task.

Check my POST on inheritance for a more detailed explanation with examples!

No comments:

Post a Comment