Class Fundamentals
Class Fundamentals
• What is a class?
• A class is a template or blueprint from which objects are
created. (OR)
• A class is a collection of data members and member
functions.
• What are data members?
• Data members are nothing but simply variables that we
declare inside the class, so it called data member of that
particular class
• What are member functions?
• Member functions are the functions or methods which
we declare inside the class , so it is called member
function of that particular class
• General form / syntax of a class:
class classname
{
type instance-variable; //data members
}
• Methods:
• Java programmers use methods to describe behaviors. A
method declaration has the following syntax:
type methodname(parameter-list)
{
// method body
}
//output
Area = 50
• Parameterized constructors: constructors with parameters
are called parameterized constructors.
class Rectangle // eg: for parameterized constructors:
{ int length;
int width;
Rectangle ( int x , int y) // Constructor method
{ length = x;
width = y;
}
int rectArea()
{
return(length * width)
} }
class RectangleDemo
{
public static void main (string args[ ])
{
Rectangle rect = new Rectangle(5 , 10);
int area = rect. rectArea( );
System.out.println(“Area = ”+ area);
}
}