ad

Wednesday, 29 April 2015

What is Object Oriented Programming and its 4 principles

Today I am going to talk to your guys about OOP Object Oriented Programming. So lets first define what OOP is, OOP is a programming style based on Objects - Object can be anything, for instance TV is an Object, and so it everything else we see in physical world. But when it comes to programming it slightly  changes any data structure can become an object. Data structure is simply a way of organizing data.

Now, you may ask whats the need to use OO style. Well here is what may surprise you, most if not all libraries are written in this style. If you ever start using one, and do not already know OOP, you are going to have hard time understanding how things are working. Another advantage of learning OOP is code organization, chances are if something has been written in well structured manner, it will be easier to re used in future.

As mentioned above object is the actual thing. But what actions are executed in order to get an object?? Well, there's something called a Class, a class is a template to make Object. Have you ever been or seen a factory? Basically the major part of it is occupied by machines, now in a biscuit factory there is couple of machines, either biscuit is made with all machines working together - each playing its own role or there can be just one machine that makes whole biscuit. In either situation, our machine is our Class and biscuit our Object.

Lets talk about the principles.

Abstraction
First of all we have Abstraction. Abstraction is nothing but a concept in OOP. Basically, a Class helps us achieve abstraction, often when someone shares code we do not need to know how things are working on the other end, all we know is if I do this, I will get this. For example, whenever you hit press a key on your keyboard, you really do not know what exactly is happening the moment you press the key, all you know is if I press a key I will see a word on screen. It has given us an interface, the button.

Encapsulation
Now we have Encapsulation, Encapsulation is pretty straightforward, it is basically packing your data so people only have access to certain things that they really need. Lets say you are making a mobile phone, user can make calls, charge, take pictures etc.. But what if user tries to change mobile phone model? its other mechanism? What if you tried charging it with different cable? Well it will not allow you to do so. Because those things are encapsulated. It only gives you access to what you need.

Inheritance
Inheritance is used a lot. Inheritance is something that inherits(comes form) something else that is more generic.You can inherit its features into something specific you may want to make. For example, you have a class that validates username, password, email. If you inherit another class from it, your new class will have all the functionality that the original class had, you would not need to copy paste features from original class to new class because you have inherited it all to new class. Now you can add more features to the new class and you have something with additional features. Inheritance allows to not repeat same code over and over again.

Polymorphism
Polymorphism is also easy to understand. I will quote this from code.tutsplus.com, it explains it quite well.
"The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way. A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button “does,” however, depends on what it is connected to and the context in which it is used — but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task.

In the programming world, polymorphism is used to make applications more modular and extensible. Instead of messy conditional statements describing different courses of action, you create interchangeable objects that you select based on your needs. That is the basic goal of polymorphism."

No comments:

Post a Comment