Thursday, January 14, 2010

Java VS Real Life

we always treat java as programming languauge, but if you keenly watch, its same as our real life.

Lets take some example:

class Person {
private String name;
priavte int age;
private int height;
public boolean doEat(){
return true;
}
public boolean doSleep(){
return true;
}
public boolean doBreath(){
return true;
}
}

public class Manikesh extends Person{

}

Above example is a normal java class, now lets relate this to our real life.
for a human being, person would be most generic form, when we say person, it might be he or she or anything. So in java,a person would be super class which defines rules.

a Person should have states or attribute as name, age, height etc.... , and person should have behaviuor as well like jumping,eating, sleeping, talking etc...states will have some value and behavour will describe what he does or what he does not.

so now if you see the Person class, the member variables of the class acts as states of a person and all member methods acts as behaviour of Person.

Manikesh is a class which extends Person in simple words Manikesh Is A Person.
when Manikesh extends Person then he will have same states and behaviour.
name=manikesh
age= 28
height= 5.7

same will go for behaviour.so any class which extends Person is a human being and will have same states and behavious where values might differ.

so here Manikesh is one instance of Person.
There are two simple key words in java IsA and HasA .

IsA examples:

Manikesh IsA/extends Person
Mango IsA/extends Fruits
Car IsA/extends Vehicle

hasA examples:

Manikesh hasA Car/bike/nose etc...
Car hasA wheels/engine etc...

Wrong combinations for IsA and hasA

car extends garage
manikesh extends car

in other words
car isA garage |-- These two statements are wrong, they dont clear isA test.
manikesh isA car |

3 comments:

  1. though im new to language programmin..watevr i knw abt is jzt a basics i hav had learnt durin colleg time..

    i like it java Vs real life..nice..itz der in d books or itz ur way to present java..

    ReplyDelete
  2. I dint read all these in any book.. its all my own understanding..

    ReplyDelete
  3. hello manikesh ur examples r really understandable i'm here in mumbai working in a IT company i'm also from chennai.now i started to learn java so pls advice me to choose the steps to follow

    ReplyDelete