Object can be refers to an actual instance of a class. Every object must belong to a class. Objects are created and destroyed within the execution of the program.
- Objects provide the benefit of modularity and information hiding.
Simply a class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind. Using object-oriented terminology, can say that a humanBeing object is an instance of the class of objects known as Human. humanBeing have some state (name, color, hungry) and behavior (talk, eat, write) in common. However, each humanBeing state is independent of and can be different from other humanBeing.
Class can be refers to the actual written piece of source code in which the class is defined. The properties of the class does not change before, during or after the execution of the program.
- Classes provide the benefit of reusability. Software programmers use the same class, and thus the same code, over and over again to create many objects.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package p;
/**
*
* @author DELMI
*/
public class Human {
void talk(String s) {
System.out.println("Human can talk " + s);
}
String skin(String s){
String skinColor = s;
return skinColor;
}
}
class perform {
public static void main(String arg[]) {
//create reference variable of Human h1 & h2
Human h1 = new Human();
Human h2 = new Human();
//initialize languages e & s
String e="English";
String s="Sinhala";
//call the methods using reference variables
h1.talk(e);
h2.talk(s);
//print the reference variales h1 & h2
System.out.println(h1);
System.out.println(h2);
//verify that a new object can be referd by the existing refernce variable
h1=new Human();
System.out.println(h1);
//verify that the class methods are not perform untile it is called
String b="Black";
String w="White";
Object o= h1.skin(b);
System.out.println("The color of skin is "+o);
}
}
//:::::::::OUTPUT:::::::::
//Human can talk English
//Human can talk Sinhala
//p.Human@19821f
//p.Human@addbf1
//p.Human@42e816
//The color of skin is Black
Read More - SCJP Sun Certified Java Programmer Study Guide, Chapter 2 Object Orientation by Kathy Sierra & http://www.iam.ubc.ca/guides/javatut99/java/