1. Program to find the area of
Rectangle with dimension
passed through arguments.
Class Rectangle
{
int l,b //attribute
public voiddisplay()
{
System.out.println(“Length=”+l);
System.out.println(“Breadth=”+b);
}
publicintarea()
{
Returnl*b;
}
Publicvoid setDimension(intx,inty)
{
I=x;
B=y;
}
}
Class RectangleTest
{
publicstaticvoidmain(Stringarr[])
{
Rectangle r = new Rectangle();
r.setDimension(20,30)
{
System.out.println(“dimensionof rectangle are”);
r.display();
System.out.println(“areaof rectangle ris
=”+r.area());
}}
Syntax for creating object of a class:
Classname refrencevariable = new classname();
Reference variable is a implicit pointer which is used to contain the
reference of objects.
In java reference variable are dynamically created. Hence they don’t
have name and are referenced through their reference variables.
In C++: there is static allocation of objects
Rectangle r ======== r
In Java:
r
l
b
2. 1. Rectangle r r
2. r=new Rectangle();
r -----------
Difference between reference Variable and address:
Address represents the actual memory locations where as the reference
variable is a mechanisms of identifying the memory location.
if we declare the main method in separate class. Then that class
is reusable(now we can use rectangle class anywhere just by
declaring its object)
null
100
100
object
l
b