Thursday, 28 June 2012

OOPs concept with Examples

For us to learn the Java technology, we need to have  a clear understanding of Object oriented concepts.

As we may  have heard many times,  C++ , Java, ...are all object oriented programs. What does this actually mean? . In  Java world, everything is considered as an object. By making your program object oriented , you considerably increase the chance of re-usability and modularity of the code. Does it not sound great that a code written by you was found more generic and could be reused in many instances just by invoking your object created.  We could possibly define any real world object with the two characteristics like state and behavior.

Lets us understand the STATE and BEHAVIOR with an illustration:

I have a pet dog at home. He is a german sheperd breed , He is brown in color. He does play with a ball , he does accompany me for a walk, he does greet friends by wagging his tails.
So here the STATE can be categorized by common characteristics of a dog like , "BREED,COLOR,GENDER"

BEHAVIOR will be WALKING,GREETING and PLAYING

so now are you able to relate what it means a STATE and BEHAVIOR.
 
In java terms, now Class will become Dog, all the STATES (color,breed,gender) will become variables and  the BEHAVIORS , "walking , greeting, playing " will become  methods.

In Java words ,

class Doggie
{
string name;
string color;
string  gender;

void doPlay()
{

}

void doGreet()
{

}

void doWalk()
{

}

}


So we are now able to relate a real world object to a software object..

Lets continue with the OOPs concepts like Encapsulation, Inheritance , polymorphism, Abstraction in the next blog.