Amazon

Thursday 14 July 2011

Class fundamental in Java

We already used class in many programs. All java program start with the class definitions. Class is the template of the object. So object is the instance of a class. When you define a class you declare it's exact form and nature. A class is declared by the keyword class.

The general form of class:

class CLASS_NAME
{
type variable;
type variable;

type methode(paramater)
{
}
}

data variable defined within the class are called instance variables. code is contained within methods all methods and variables of a class are called members of that class.

Simple class


class Box
{
double width;
double height;
double depth;

Box() // constructor for initialization
{
width=5.0;
hieght=5.0;
depth=5.0;
}

double getVolume()
{
return width*height*depth;
}
}