A class is simply a template.
An object is created to give life to the various attributes of a class..
Note:- In Java a class template takes no memory, Memory is allocated when object is created.
Program :- Calculate Area of Rectangle
An object is created to give life to the various attributes of a class..
Note:- In Java a class template takes no memory, Memory is allocated when object is created.
Program :- Calculate Area of Rectangle
class Rectangle
{
double width;
double height;
double area();
{
return width height;
}
}
class RectangleDemo
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle();
double area;
rec1.width=10;
rec1.height=20;
area=rec1.area();
System.out.println("Area"+area);
}
}
OUTPUT:-
Area 200.0
Note :- The line written in yellow line is used for creating object of a class..
where
rec1- Reference variable
new- New operator
Rectangle()- Constructor
No comments:
Post a Comment