This article intends to be a primer so that you may gain insight in order to understand beginner OOP articles better (for example on wikipedia). In order to grasp more abstract concepts, suspend as much logic as you would when watching Star Trek.
Message not sponsored by anyone I know. Some neurons dissipated during the writing of this.
A programming paradigm is a style philosophy to implement structure and design in programs. There are different types of programming paradigms or philosophies. These paradigms have different conventions and ways of going about things. There are different types of paradigms, a few examples are : declarative, procedural, OOP, functional.
In a way, OOP is like fashion style for developers. The type of style philosophy you apply depends on what you’re trying to do and developer tools and options available to you.
In my previous post I described what an object is. Like stated above OOP is a style or design philosophy. It requires practicing a new way of thinking. This design oriented thinking takes objects as its main focus to design applications. In OOP a programming problem or end goal is thought in terms of objects whose properties have states (e.g. on and off) and behavior (clicking, rolling etc). Therefore programming consists of manipulating and being able to reuse objects.
A more formal definition of procedural programming is:
“Procedural language is a type of computer programming language that specifies a series of well-structured steps and procedures within its programming context to compose a program. It contains a systematic order of statements, functions and commands to complete a computational task or program.
So in newbie terms what this means is that you’re using variables with parameters and passing them as arguments in functions, followed by a sequence of events (usually by using if, else , while , for etc) to get to the desired result.
The chorus for procedural programming song is: “First Do This, Next do This”.
And here is the Rant:
In procedural programming you must go very linearly (101.9999% instead of the usual 100.9999%) as you have to first specify all that you must use and then in what order they are to be used. This is leads to an ummm..and oh… moment. In OOP (object oriented programming) you create objects with properties and then can get to to do different things, or reach different states without having to worry about specifying everything right at the onset or for that matter worrying too much about the sequence of events in the beginning. . If this didn’t make sense to you that’s fine, it’s just me rambling. If you’ve used an OOP language in a procedural manner you will appreciate this rant.
Coming back to OOP
These are the a few basic concepts behind this new paradigm. I’m going to try and make it succinct without trying to fizzle out your neurons:
1. Objects: I’ve described briefly previously, in that objects can be thought in terms of their properties. The idea behind thinking about objects like this is that objects in OOP are to be treated like real world objects that can be manipulated to achieve different states (which you do by functions and methods). Methods modify an objects behavior by manipulating their properties, more on them in another post as this is all that’s needed for now.
2. Classes: The dictionary definition for a class is: ” a set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality”. Keeping this definition in mind, in OOP a class is a template for creating objects. Think of it as a blueprint. For example if you have a 3d printer, then you need the most basic design for printing a cup. After printing out a couple of cups you can then decide what color each individual cup should be, what design you can imprint on it etc. Classes consist of the initial state & template for a state (e.g: on, off) and initial state & template for behavior ,through methods and functions (e.g drop down).
function shoppingTemplate ( );
Inheritance allows objects to take on the properties of existing objects and classes. So in other words you are using a class or object as a basis for another class or object.
In the same way objects interact with other objects through messages and all that they know about each other is the what the other one looks like i.e. the interface. Each object’s data and logic is hidden from the other. This allows you , the developer to separate how you make the object behave (aka implementation) from its actual behavior.
In OOP, the principle of polymorphism is “ the ability of different objects to respond to the same message with different answers.” (more here). An analogy of polymorphism: A school bell rings, students depending on their schedule will react differently, some head to the library, some home, some go to play football etc (gotten from here).
An example of this analogy in OOP is that a function getEmployee will return different data types depending on whether it was used on var employeeNumber which will return an integer or var employeeName which will return a string. This type of polymorphism is known as overloading.
Why should I bother with OOP ? Textbook speak ” Advantages of OOP” :
1. Objects in OOP mimic real like objects this reduces complexity and we avoid the very common and confusing convention found in life sciences – naming things after the person who discovered them which might have very little to do with the actual concept.
2. Building objects which can be extended to other programs therefore, be reused as is or be modified.
3. It’s easy to maintain and add new code.
More newbie Reading: