Thursday, January 29, 2015

OOP Concepts

Why to use OOP? And why it came into existence? What was the drawbacks of procedural programming paradigm?

Let’s take a Real world example and see how the problem is solved using both the concept?
Imagine you are working in a Vehicle manufacturing company where you need to keep track of inventory system (i.e. parts). The problem is the company manufacture parts for both Car and Trucks.
For cars, you will need to have information about:
  • Color
  • Engine Power
  • No of gears
  • Number of doors
Similarly for trucks you will need same information with some modification as:
  • Color
  • Engine Power
  • No. of gears
  • Capacity of Truck

How to proceed with the design in procedural programming:

In procedural programming, you need to design different programs for both car as well as trucks. (Even a slight modification in the requirement forces you to design it again completely from starting)
This is where procedural programming suffers a drawback.

Now with Object Oriented Programming (OOP)

The problem is much easier than it seems. All you need to do is find out the requirement which you found common for both; wrap it up in the base class. In the above problem we can wrap function (Method) for Color, Engine Power, No. of gears inside the main class i.e. Vehicle class. Next we will create two more classes i.e. one for Car and other for Truck both of them will inherit the feature of main class i.e. Vehicle class. Now for the method which is required only for Car i.e. Number of doors, we can add it into the Car class separately and for truck we need add the method Capacity of truck into the Truck class all the other method are inherited from the Vehicle class (i.e. base class or parent class or main class).
Note: both Bus and Truck class inherit the base class i.e. Vehicle class so they can access all the method specified inside the Vehicle class.
What actually we did in OOP programming is that instead of creating different program to achieve slightly different requirement we divide the whole program into classes and with the feature of Inheritance in OOP we inherit the functionality of one class into other.
The best thing about OOP is that; if say there comes a requirement in future and we need to make some changes in the program, let’s say we need to add one more product i.e. Bus into the existing system. All you have to do is just create a class Bus and inherit the property of Vehicle class into it and add the extra method required only for the Bus class.
Phew, I hope that you now know the importance of OOP and why OOP is preferred over procedural programming.

Now let’s talk about the 4 Pillars of OOP in detail

1) Abstraction

Definition:  Abstraction is the process of hiding out the working style of an object and showing only the required information of the object in understandable manner.
What does it actually mean?
Let’s discuss it with the help of an example.
Smartphones. 99% of you might be having a smartphone with you and the rest 1% at least know what a smartphone is.  You use your phone to call, text, play game, surf on the internet, even now you may be using your smartphone to read this article. So my point is do you actually know how it’s happening and which method and which function your phone is calling to carry all the task for you. You don’t and I am quite sure about that. This is what we call Abstraction, hiding the complexity and only allowing you to view the information of the object in understandable manner.
Let’s have another example of Car. You know that you need to put on the keys, change the gear and accelerate in order to drive a car but do you actually know what happens when you change the gear and accelerate, or say how the car engine works? You don’t, and that’s simple because you don’t need to know all that complexity. Your Job is to just drive the car. That’s what we call Abstraction.

2) Encapsulation

Definition:  Encapsulation means wrapping up data and member function (Method) together into a single unit i.e. class. Encapsulation automatically achieve the concept of data hiding providing security to data by making the variable as private and expose the property to access the private data which would be public.
Let’s discuss Encapsulation with a example
Let’s take a simple example of BAG, yes your school and college Bag in which you keep all your stuff like books and pens and document etc. to keep it safe from the outside world. Here you are hiding your books and pen by putting them inside the Bag similarly in OOP we encapsulate the data and methods inside the class to keep it safe and accessible only to authorized member.

3) Polymorphism

Poly=many morph= form
By name we can come to a conclusion that it’s about something with many forms. In OOP it is an ability of an object to take many forms.
Let’s learn with the help of an example:
My name is Ankit: I am a brother, a friend, a son, a student depending upon the situation i.e. parameter surrounding me. Similarly in OOP we can have a function with different form but with same name. Do note that the name is always same. The factor which distinguishes one method from the other is parameters.
Let’s say a method to calculate square() of a number, now the number can be of integer type, float type, or double.Based on the parameter type passed jre automatically calls the suitable method . This concept of OOP is known as Method Overloading.
  1. class MethodOverload
  2. {
  3. int n;
  4. float f;
  5. double d;
  6. void square(int a)
  7. {
  8. n=a;
  9. System.out.println("Square of "+n+" = "+n*n);
  10. }
  11. void square(float a)
  12. {
  13. f=a;
  14. System.out.println("Square of "+f+" = "+f*f);
  15. }
  16. void square(double a)
  17. {
  18. d=a;
  19. System.out.println("Square of "+d+" = "+d*d);
  20. }
  21. public static void main(String arg[])
  22. {
  23. MethodOverload obj=new MethodOverload();
  24. obj.square(5);
  25. obj.square(5.9f); //In 5.9f 'f' defines that its an float type value
  26. obj.square(4.8); //java automatically takes any value with decimal point as double type
  27. }
  28. }
You can see this much detailed post about Polymorphism in Java and its Type.

4) Inheritance

Inheritance is when an object acquires the property of another object.
We already talked about inheritance in detail while explaining the Car and Truck example, Scroll back to the top and read the example again. In order to ease the coding process we are inheriting the properties of parent class into the child class. Inheriting the property of one class to other is known as Inheritance.
Check this out much detailed post on Inheritance in java.
Different types of Inheritance:
  • Single Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
Different types of Inheritance
Note: Multiple Inheritance is not supported in Java instead we make use of Interfaces to achieve the functionality same as of multiple inheritance in JAVA. Read this article on why multiple inheritance is not allowed in java.

No comments:

Post a Comment