Class


Class is a template for an object.

General form of class includes:

class classname
{
                       type instance variable1;
                       type instance variable2;
                       :
                       :
                       type instance variable;
                       type methodname1(parameter-list)
                       {
                       //body of method
                       }
}

The data,variables defined within class are instance variables. Each method can be used to apply a particular function. The variables and methods defined within a class are members of a class.

Variables within a class are called as instance variables because each instance of class
(i.e each object of class) contains its own copy of these variables.Thus, the data for one object is separate or unique from data for other.

Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform.

A Simple class

class S
{
int a;
float b;
double c;
}

A class defines a new type of data, the new data type in this case is called S.You will have to use this name for objects of  type S.

Preceding code does not generate objects.To actually create objects we use following syntax:-
S d=new S();//create S type of object called d
d.a=10;
The above statement creates the object of class S and assigns value 10 to variable a.
Previous
Next Post »