Sunday, July 22, 2012

JAVA CLASS & OBJECTS

Java thinks in terms of objects. Lets take my example...
I am a person. So Person becomes a class.


I have my name, age, gender, address. These are my attributes which become properties/class variables of class.


I walk, eat, sleep, run, work etc., These are my behaviors, which becomes methods/functions of a class.


public class Person // [access modifiers] [class - the keyword] [class_name]
{
// [access modifiers] [datatype] [variable_name]
public String name; 
public int age;
public char gender;


[access modifiers] [return type] [method_name]
public void walk()
{}


public void eat()
{}


public void run(int kms)
{}


}


Now obviously, I AM (not) LEGEND. I communicate with lot other objects. e.g.., Organization (my company), other Persons, my Pet, my Vehicle, my Bank etc... So the objects I communicate with may similar objects (like Person) or objects of other type (like Vehicle, Pet, Bank etc..).


public class Dog
{
public String breed;
public String name;
public float weight;


public void bark()
{}


public void play()
{}


}




public class Bank
{
public String bankName;
public String address;


public void openAccount()
{}


public void transferMoney(float amount)
{}


}





No comments:

Post a Comment