❮ Previous Next ❯

Class And Object in JAVA:

Class is the core concept of java.

Class Declaration:

Class classname
{
field declaration;
Method declaration;
}

Example

class SilanSoftware
{
//fields declaration
int x;
double y;
//methods declaration
void getData( );
void display( );
}

The above example represents that the keyword class tells to the compiler that SilanSoftware is a class taking two fields such as x and y and two methods such as getData( ) and display( ).
Let's see a simple program:

class Test
{
double length;
double breadth;
void getData()
{
length=10.5;
breadth=5.4;
}
void area()
{
double a;
a=length*breadth;
system.out.println("the area is"+a);
}
} class RectangleExample
{
public static void main(String args[ ])
{
Rectangle obj = new Rectangle();
obj.getdata();
obj.area();
}
}
OUTPUT

the area is 56.76

Object:

Creating more than one object:

Example

class RectangleDemo
{
int length, breadth;
void get(int x, int y)
{
length=x;
breadth=y;
}
void area()
{
int a;
a=length*breadth;
system.out.println("the area is:"+a);
}
}
class Test
{
public static void main(String args[ ])
{
Rectangle rec1= new Rectangle( );
Rectangle rec2= new Rectangle( );
rec1.getdata(10, 15);
rec1.area( );
rec2.getdata(20, 25);
rec2.area( );
}
}
OUTPUT the area is 150
the area is 500 ❮ Previous Next ❯


  Tutorials

Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Bhubaneswar-751015
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved