Introduction to Object Oriented Programming with Java

There is a famous story about a professor who goes to meet a zen master inquiring about zen.
The zen master welcomes the professor to his house and they sit to have tea. The master starts pouring tea into the professor's tea cup and keeps pouring long after it has filled up. The professor watches it at first with curiousity but the master oblivously keeps pouring and then no longer able to restrain himself the professor speaks out "the cup is overfull. No more will go in" and then the master says "it is the same with your mind. You have filled it with prejudices and preconception, how then is it possible for me to show you zen?"

Similar is the case with OOP. When we learn this style of programming, we must give up all that we had learnt in the procedural languages like C, fortran etc. OOP is a refreshingly new style of programming and requires a whole new way of thinking to be implemented. What this new way is, we shall approach shortly.

Now consider the following scenario.
We have the following people from a community
Jerry- a team leader
Kate- a negotiator, and resource manager
Chirs- a software programmer
Dan- a senior software consultant
Mira- an accountant and steno
Zoe- an automobile engineer
Mike- an electronics expert

Of course there are other people in the community but these are the ones who shall be in our view for the present.
Excepting Zoe and Mike, all the rest make up a software firm that provides high end software solutions (including certain hardware) and of course as you might have guessed, Jerry leads the team.

Now what happens is that an automobile company apporaches Jerry and asks him if they would design and program a chip that would make their latest automobile a versatile and artificially intelligent, robust beast.

Of course Jerry is all too glad and consents. The company then hands over the blueprints of the car and all the design and implementation details.

Jerry gets in touch with Dan and Chris and hands over the details to them. He further asks them to hire the people they will require. He then goes to Kate and asks her to meet up with Chris and Dan and get them what they need at the best price.

Meanwhile Dan and Chris decide to hire (of course) Zoe and Mike (who are their friends) and call them up. The four team up and have a brainstorming session on how to go about with it. With Zoe and Mike's expert advice, Dan gets a good insight into the software and hardware requirements to resolve the problem at hand and suggests them to Chris.

Now Kate comes to Dan and Chris and ask them about their requirements. The two tell Kate about Zoe, Mike and all the stuff they'll need. Kate then goes to Zoe and Mike, negotiates for a good price and hires them. She then fetches the rest of the requirements from the company's regular store.

Now the four intellectuals sit together and in a months time, most of the desiging and planning is done, in another month, Mike gives the specially designed chip to the other three and leaves. His work is complete for now. The rest of them now go deeper into the intricate mapping between the software programming that needs to be done and the actual physical effect it has to have on the car. In a couple of months, the Mapping is ready and it is Zoe's turn to leave. The two are left with all they require to start programming the chip and with expert advices from Dan, Chris does a great job of it using certain detailed implementations of technologies X, Y and Z. In four months, the whole thing is ready. Now Chris and Dan sit together and make a detailed report of their product. They then call Mike and Zoe to help them complete the documentation especially things like why they chose the design/technologies they chose and about how the chip must be integrated with the automobile. And one fine day, its all ready.

The whole thing is then rechecked and submitted to Jerry. Jerry then asks Kate to sit with Mira and get the whole accounts updated and get him the final bill statements. Kate does this and gives Jerry the final bill. Jerry then goes through the thing, adds in some things like development charges, consultation fees, and all those things that will actually make up for their salary.

He then goes to the automobile company with the programmed chip and the documentation (which includes the manual) and submits them along with the bill. The engineers go through the whole thing, approve it and Jerry is paid. What goes on after that is more or less inconsequential to our interests here.

Ok, thats the whole thing. Now I'll ask you a few questions
1. When Jerry gives the blueprints to Chris and Dan, what does he ask them to do?
He asks them to design the required chip and hire the people they wanted right? Now tell me did he know what the final thing was going to be like? did he have any idea about what the guys were gonna do and give him? nopes, he just knew that all he was going to get was the "REQUIRED CHIP" and he didn't know what kind of people he was going to hire either. He just asked them to hire "REQUIRED PEOPLE"
Look at that! the leader doesnt know these things, but he still gets the work done.
What Jerry has done is in essence ABSTRACTION and things like "required chip" and "required people" are abstract items or quantities, for without specific information, they do not exist physically.
For example- there is no chip called required chip until I specify the brand and processor speed or put down all the configurations or make it so specific that there is no amibguity about what I want.

The main thing to remember about abstraction is, it gives a definition to what we are looking for- like a "dream ship" or a "perfect girl" but it never bothers to be specific because there are many things that would fit into what you are looking for and many of those would satisfy you. In the above scenario, there could have been several other chips that could suit the purpose, other than what Chris and Dan designed.

In fact the main PURPOSE of abstraction is to carve out a space within which we shall go about finding solutions for the problem at hand.

Let us take a deeper look into this
Alan feels something is missing in his life and wants to find the solution, so he meditates upon it and realises that he needs "PEACE OF MIND" <-there we got our abstract quantity
So basically, Alan figures that the problem has a solution in "PEACE OF MIND"
He then writes on a piece of paper.

PEACE OF MIND
IS
----------FREEDOM FROM WORRY
----------FREEDOM FROM FEAR
----------FREEDOM FROM THOUGHTS ABOUT PAST
----------FREEDOM FROM NOISE
----------FREEDOM FROM DESIRE
etc.
Then he writes
FREEDOM FROM WORRY
----------KNOWING THAT WHATEVER HAPPENS IS FOR THE BEST

FREEDOM FROM FEAR
----------KNOWING THAT THERE IS SOMEONE ABOVE TO TAKE CARE

FREEDOM FROM THOUGHTS ABOUT PAST
----------FORGIVING THOSE WHO MADE LIFE BAD FOR YOU

and so for the others
Can you see what is happening? Starting from an abstract quantity, Alan is moving to a CONCRETE solution to his problems.
After two to three levels further going down, Alan catches the points in his life where things were not going according to his definition of PEACE OF MIND, and could immediately correct it.
BY ABSTRACTING THE PROBLEM, THE SOLUTION COMES EASILY TO US
And that is the major use of abstraction. It makes solutions come to us with ease, and more importantly in a well-defined and structured manner.

And remember you can only ASK for an ABSTRACT QTY. and NEVER CREATE it. This principle we shall be using lots of times later, so keep this in mind.

Now lets get back to the first scenario.
Did Jerry KNOW how the thing was going to be done and that Chris and Dan had hired Mike and Zoe? Of course not, why would Chris and Dan bother Jerry (who's basically a management guy) about all the software, electronic and automobile engineering jargon? Jerry didn't need to know (period) Neither do the automobile company guys. And Zoe and Mike know nothing about how Chris and Dan programmed the chip.

That is called ENCAPSULATION- or in other words HIDING DATA & IMPLEMENTATION. But why? Firstly to keep things as independent as possible and secondly to avoid unnecessary butting in. In more software like words, to preserve reusability (by making it independent) and avoid data corruption and prevent illegal actions.

Consider tha fan regulator in your room. It is actually much more than that sleek knob that you see, instead it is a pretty complex circuit within and you get to see none of it. It exposes only two interfaces (here interface just means something that can interact) for manipulation. The two wires behind the board for power input and the knob for regulating output. Thats it. You don't need anything more to use it. This is a classic example of encapsulation. Imagine if the whole regulator were open, any tom would be able to tamper with it and maybe get you an electric shock or short circuit the whole power supply! Meditate on this, and you'll know why you need encapsulation.

I'm reminded of an incident in my life when I had to submit a presentation at school and I was taken ill. So I had asked my dad to do it for me just so that I could escape the fury of the teachers. And my dad was kinda busy but he said he'd do it. And sometimes I'd get delirious about whether the thing got completed or not. And one day when I caught my dad, I asked him how far is it complete? and he twitched his lips and said "Why do you worry? I've told you I'll do it and I'll do it, how I do it, and when, just don't bother about these." And I set my mind at rest.and sure enough, the thing was ready on time. Another example of encapsulation, and another use- it prevents unnecessary worry, just trust things to know how to take care of themselves.

The above two concepts were explained mainly as an introduction, the real thing in java, we shall when we get down to writing code.

Two other concepts that are part of OOP are Inheritance and Polymorphism.

These are very easy concepts to understand, the names themselves speak a good deal. We shall suffice ourselves with just an general understanding of the two, the more detailed and precise definitions we shall give later.

Isn't there something common between your dad and you? or your mom and you? if yes, then you probably Inherited those features/qualities from your parent.

Consider a Shape.
A Rectangle is a Shape
So is a Circle

Let us draw an abstract to less abstract chart of Shape

SHAPE (something with a boundary)
----------POLYGON
-----------------TRIANGLE
-----------------QUADRILATERAL
---------------------------PARALLELOGRAM
-----------------------------------------RHOMBUS
-----------------------------------------RECTANGLE
--------------------------------------------------SQUARE
-----------------PENTAGON
-----------------ETC.
----------CURVED SHAPES
-----------------ELLIPSE
----------------------CIRCLE
--------------------etc.

Can you see whats going on? A square is a rectangle and also a quadrilateral and also a polygon and also a shape. Any figure down the hierarchy is a more concrete shape- more importantly, they are all shapes. And since they are all shapes, everything that a shape has, they also have. Since a shape can be filled with color, so can any of polygon, circle etc. In other words, since these things are DERIVED from the basic BASE called SHAPE, they have ALL the properties of a shape. This is essentially INHERITANCE.

INHERITANCE is when less abstract classes (called the DERIVED CLASS) of a BASE class DERIVE ALL the properties and functionality of the Base class

A shape can also be rotated, therefore so can a rhombus or a triangle. But when we want to rotate a shape, we will have to do it depending on what shape it is exactly right? A circle is rotated about the axis and a rhombus around the diagonal. BUT, the BASIC OPERATION is ROTATE, and rotate is what we shall do. But again, it DEPENDS on what exact shape it is. Therefore a circle has a different WAY to ROTATE and so does a rhombus.

When different derived classes behave differntly when the same operation is performed, it is called POLYMORPHISM- i.e it is morphing (changing) in many ways (poly).

We shall stop with a funny kind of polymorphism but this is just for fun, the accurate version is the one above.

Every morning I go out for a walk and it happens that I come across quite a few people I know. What happens when you come across people you know? they probably greet you- now this is the way each of them greet me.

Uncle- hello sonny, how're you doing
Girlfriend- Heyyy (with a lil honey in her voice, and a wink) whats up dude
Friend- whats up buddy, whats your plan for today?
Close friend- Whats up *#$%& ( with a whack on the back - no offence intended)
Angry Classmate- (venom in his eyes) grrrr
The Colony Grouch- -sNoRt-

Well, they're all people, but because they're all different, they do the same thing (greet) in different ways. Thats polymorphism too!

created by Robinhood

1 komentar:

Riski Syendi mengatakan...

kita juga punya nih artikel mengenai 'object oriented programming', silahkan dikunjungi dan dibaca , berikut linknya http://repository.gunadarma.ac.id/bitstream/123456789/629/1/B93-99_Melani_S.pdf
terimakasih

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
This Theme Modified by Kapten Andre based on Structure Theme from MIT-style License by Jason J. Jaeger