Class and Object

Look around you and you find a lot of objects. The world around us is actually full of objects. The chair you are probably seated on, is an object. The table nearby is an object. You infact are also an object. Now go one step ahead and notice that every object is of a particular type. Notice and you find that all chair share some common characterstics that seperate them from table. Same way all table are different from human. So all chairs are of one type, all tables are of another type, right? Now lets suppose you have 3 chair in your room: chair1, chair2 and chair3. Also, you have 3 tables in your room: table1, table2 and table3. Now notice, all chairs are of one type or you can say all chair have a class and same way all table have a class. Here, chair1, chair2, table1, table2 etc are objects. But Chair or Table itself is a class. So Chair is a class and chair1 is one particular existance of that class (so chair1 is an object of class Chair). Similarily, if you consider 2 dogs : Tommy and Sheru. Both of them are objects, and what is their type? They are of type Dog, or you can simply say they are of class Dog. So they are object of class Dog. Hence, 'Dog' is a class and there can be many objects of that class : Tommy, Sheru, Lucy, Sasha etc. Java also has same concept when you talk about class and Object. A class is a type of something, a kind of blueprints or prototype, that defines the common attributes of the objects. An object is an instance (or one particular existance) of that class/type. Now notice, every object has 2 things: it has a behavior and it has a state. Behavior is something the object does: the dog bark, the dog walks. so barking, walking defines behavior of dog. State is something the object possess at one moment of time. The dog has some weight and height, it has a particular color. So value of weight, height and color at some particular instance of time defines state of the dog. All dogs speak same way (i.e. they bark), all dogs walk same way. But all dogs will have different color or weight. So, all objects of a class have similar behaviour but they have different state. In java, the behavior is defined by method of class. The state is defined by class fields/variables. The methods or fields of a class are called its member. Now lets looks at the syntax of how you define a class in java and how you create and use object. class Dog { int height = 4; String color = blue; String name = Tommy; void bark(){ sysout.out.println("Bow Bow. I am " + name); } } How to create objects of class Dog object1 = new Dog(); Dog object2 = new Dog(); object1.bark(); object2.bark(); Output: Bow Bow. I am Tommy. Bow Bow. I am Tommy. Now notice one thing, you created two different objects of Dog. But both are same, both are Tommy. Though you wanted one to be Tommy and another to be Sheru. Thik!!! Why did this happen? How do you create different dogs.... a Tommy and a Sheru? Now look at the next program and there we will create 2 different dogs. --------------- Start next page. --------------------------- class Dog { int height; String color; String name; Dog(int height, int color, int name){ this.height = height; this.color = color; = name; } void bark(){ sysout.out.println("Bow Bow. I am " + name); } }