Monday, December 8, 2008

Principle of OOPS

Welcome back to Java Code Online.

Object Oriented Programming(OOPS) is the basic heart of JAVA. Java is built on the principle of OOPS. The name OOPS explains its meaning. It implies that in Java everything is an Object. You need to think of it in the same lines.

For the persons who are new to Java technology, face some problem in visualizing things as an Object. For making it more clear, I will start from the scratch.

There are basically two approaches to programming. They are:-
1. Process Oriented
2. Object Oriented

Most of the languages are Process Oriented, the most common example of this is the language C. I will explain both of them now.

Process Oriented
A Process Oriented language is one which stress on the process. The exact word for the process here will be behavior. Like whatever an Object does is its behavior. I will explain it better with the help of an example later in this article.

Object Oriented
An Object Oriented language thinks of everything as an Object or as an entity in itself. The best part is that it makes the entity important then its behavior. Like if a person drives a car, then he thinks of the car as an Object and is not concerned about its behavior like the braking system, the steering, the automatic ignition, etc. All these process or the behavior of the car are taken car by itself. The person here is just concerned about the Object car.

I will take an example that will clear the situation further. Suppose that a developer is given the requirement that he has to implement three shapes one square, one rectangle and one triangle. Each of these when clicked, rotates and plays a sound. Then the two approaches will proceed as follows.

Process Oriented approach stress on the behavior that is rotate and play sound. So in this approach the developer will create the procedures rotate(shape) and play(shape) and he is done. Whatever be the shape just pass it to the procedure and it will make it rotate or play the sound.

Object Oriented approach will proceed with the Objects concerned. Here the developer will see that he has got three separate Objects, a square, a rectangle and a triangle. He will make separate classes for each of them and write the methods rotate() and play(), and he is done. Anyone wishing to call a particular Object can directly call the particular class.

At first glance it seems that Process Oriented is better then Object Oriented, but it is the other way. I will tell you how. The Process Oriented approach is fixed, what will happen when there is a requirement for a new shape that rotates and play sound in a different manner then the previous ones, you never know when the requirements change in this world. In this case the developer will be stuck, while the developer following Object Oriented approach will just make a new class for the new shape and write the code for its rotation in the particular way and play sound in the particular way.

So the main thing that is clear is that with Object Oriented approach, more flexibility is achieved. Now regarding making a new class each time for an Object is a bit tedious, but OOPS has a special way to simplify it. It is called abstraction.

In fact OOPS is like platform which is supported by four pillars. And the pillars are:-
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

I will discuss each of these pillars in the later posts. If you like the article then please do leave a comment, I will love to hear from you. For more information on Java keep buzzing Java Code Online.

Related Articles
What is Abstraction
Advantage of Abstraction
What is Object

1 comment:

  1. Thanks for the concepts. I never know about the two approaches, you mentioned above. i always used to think about OOPS is the only way. Good comparision is done for both the process oriented and object oriented approach. I would be waiting for more info on abstraction, inheritence, encapsulation and polymorphism in your further articles.

    ReplyDelete

Note: Only a member of this blog may post a comment.