What is class and object ? How to create object and reference of class ?

Class is a user defined data type. A class is a template for an object and Object is a
instance of a class.

A class creates a logical framework that defines the relationship between its members. When
objects of a class are creating an instance of that class.

Thus, a class is a logical construct. An object has physical reality. An object occupies
space in memory.

General form of class :
class classname
{
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodname2(parameter-list)
{
// body of method
}
// ...
type methodnameN(parameter-list)
{
// body of method
}
}
Example :
class Box
{
}
class DemoBox
{
public static void main(String args[])
{
Box mybox; // declare reference for object name
mybox = new Box(); // allocate memory of object
Box mybox1 = new Box(); // declare reference and allocate memory simultaneously
Box mybox2 = new Box(2,3,4); // declare, allocate and initialize simultaneously.
mybox2.display( ); // calling method of class using object
}
}


Comments