- Published on
OOP Concepts
- Authors
- Name
- Tony Mamedbekov
- @tmamedbekov
Object Oriented Programming is the way of thinking, is basically an approach. Is this the only approach? No!
When computers were invented programming language was invented also. The first Machine Language came on the horizon. It was basically a set of instructions for that particular machine. Machine Language is nothing but sequence of bits (0011 01 0101). If you have to build an application for a mobile device, and you are asked to build it in the Machine Language, it will be very difficult. So next we have Assembly Language Programming.
Assembly Language Programming made a little more sense (e.g. “Add A,B”, “Move B, C”, “Jump 9000”). If you had to write an inventory program for a big retailer with Assembly Language, you would not be able to do it. So the next language that came into play was Structured Programming.
Structured Programming is also referred to Procedural Programming. This language started appearing in the picture in late 1970’s. One of them was “C”. So the C language was basically dividing big logic into a set of functions. Main emphasis was focused on the functionality not data.
In real life in order to get functionality we seek objects. In programming we should be able to represent real life objects in our code. You should not only think about functions and global variables. So by representing real life objects in your code, you create objects. And this is the way of simulating real life things into your code, is called Objected Oriented Programming.
Now lets talk about object oriented programming. What are the major aspects of object oriented programming. You can refer to them as major pillars of OOP.
Object – is a piece of code, that represents a real life thing, or real life entity. So every real life thing, has two properties. They are the following (attributes or you can refer to it as characteristics and functionality or you can refer to functionality as behavior).
Class – class is a specification of an object. Attributes and Behavior in the class are related.
Abstraction – is taking the necessary details and ignoring the rest, in order to reduce the complexity and increase the efficiency.
Sample Class in C++:
class Student
{
public:
double studentID; // Student ID
double name; // Student's name
double grades; // Student grades
};
Encapsulation – is a concept that binds together the data and functions that manipulate the data, and keeps both safe from outside interference and misuse.
Encapsulation is basically a protection of data. So in order to protect certain data we will have to add access specifiers, within boundaries of the class. So you will go ahead and make it private, and it will be accessible within that certain Class only. So then you can ask what is the point of having a private data, then the class would be useless. Well in this case we will have functions in that class that are public. So that the outside code can access the functions. So you give the access to data in a controlled method. If you would like to access the data, you call the function. Public functions can be refer to as interface.
class Student
{
private:
double studentID; // Student ID
double name; // Student's name
double grades; // Student grades
public:
get {};
};
Inheritance – when an object or a class is based on another object or class using the same implementations.
In real world everything works in relationship (e.g. Country > Citizen, Father>Child, Husband>Wife) in programming world in order to simulate the relationship is simulated using inheritance. Data from one class to another class can be used, also each class will have their own data and functionality. It is not just re-usability, it is re-usability with extensive data. You can build upon what has been given to you from another class. Inheritance is when one object acquires properties of another object through relationship, and can extend the properties.
Polymorphism – is the ability to process objects differently depending on their data type or class.
So lets make an example of real world situation. Imagine you go to a grocery store and you buy vegetables (e.g. Tomatoes, Cucumbers, Potatoes, Spinach, Celery) so all of these vegetables eventually are in your kitchen so lets say your wife or your husband asks you to wash them. Well you will go ahead and wash all the vegetables, but you already know that each vegetable needs to be washed differently, for example potato needs to be washed a little more with a little pressure applied. So in order to polymorphism to be applied all of the vegetables need to be in the relationship, or part of the common class or category.
Hopefully this was helpful!