Class and Object program in java

Class and Object

Content


1. Simlpe function

public class  MethodClass
{
public static void main( String args[ ] )
{
A ob = new A();

ob.displayMsg();
}
}

class  A
{
public void displayMsg()
{
System.out.printf( "Welcome to Java\n" );
}
}

Output:

Welcome to Java



2. Program to explain Simple class

class  Distance
{
   int feet;
double inches;
}

class DistanceDemo
{
public static void main( String  args[ ] )
{
Distance  d1 = new Distance ();
Distance  d2 = new Distance ();

d1.feet = 14;
d1.inches = 9.5;
d2.feet = 19;
d2.inches = 5.5;

System.out.println( "Dist-1 : " + d1.feet + "\'-" + d1.inches + "\"" );
System.out.println( "Dist-2 : " + d2.feet + "\'-" + d2.inches + "\"" );
}
}

Output:

Dist-1 : 14'-9.5"
Dist-2 : 19'-5.5"




   
   

3. Program to explain Static or Class Method

public class  StaticMethodClass
{
public static void main( String  args[ ] )
{
A.displayMsg();
}
}

class  A
{
public static void displayMsg()
{
System.out.println( "Welcome to Java" );
}
}

Output:

Welcome to Java

   
       
           
           

  
           

4. Program to explain Setter & Getter method with string variable class

public class  SetterGetterClass
{
public static void main( String args[ ] )
{
A ob = new A();
ob.setMessage("friends");
System.out.println(" Welcome to " + ob.getMessage() + " ..!");
}
}

class  A
{
private String message;

public void setMessage( String msg )
{
message = msg;
}

public String getMessage()
{
return message;
}
}

Output:

Welcome to friends

           
           
           
           

5. Program to explain Default Constructor

class  Circle
{
private double radius;

Circle()
{
radius = 10.0;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CircleConstructor1
{
public static void main( String args[ ] )
{
Circle c1 = new Circle();

System.out.println(" Area of Circle : " + c1.area());
}
}

Output:

Area of Circle : 314.0

           
           
           
           

6.  Program to explain Parameterized Constructor  ( Constructor with arguments ).

class  Circle
{
private double radius;

Circle( double rad)
{
radius = rad;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CircleConstructor2
{
public static void main( String args[ ] )
{
Circle c1 = new Circle(10);

System.out.println(" Area of Circle : " + c1.area());
}
}

Output:

Area of Circle : 314.0

           
           
           
           
           
           

7.  Program to explain Parameterized Constructor with this reference class

class  Circle
{
private double radius;

Circle()
{
radius = 0.0;
}

Circle( double radius)
{
this.radius = radius;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CircleConstructor3
{
public static void main( String args[ ] )
{
Circle c1 = new Circle(10);

System.out.println(" Area of Circle : " + c1.area());
}
}

Output:

Area of Circle : 314.0

           
           
           
           
           
           
           

8. Program to explain Parameterized Constructor
with Object as Argument  with Circle class

class  Circle
{
private double radius;

Circle()
{
radius = 0.0;
}

Circle( double radius)
{
this.radius = radius;
}

Circle( Circle ob)
{
radius = ob.radius;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CircleConstructor4
{
public static void main( String args[ ] )
{
Circle c1 = new Circle( 10 );
Circle c2 = new Circle( c1 );
System.out.println(" Area of Circle : " + c2.area());
}
}

Output:

  Area of Circle : 314.0






9. Program to explain Calling a Constructor from another Constructor using this

class  Circle
{
private double radius;

Circle()
{
this(10);
}

Circle( double radius)
{
this.radius = radius;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CircleConstructor5
{
public static void main( String args[ ] )
{
Circle c1 = new Circle();

System.out.println(" Area of Circle : " + c1.area());
}
}

Output:

Area of Circle : 314.0






10.  Program to explain Passing Object to method

class  Circle
{
private double radius;

Circle()
{
radius = 0.0;
}

Circle( double radius)
{
this.radius = radius;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CirclePassingObject
{
public static void main( String args[ ] )
{
Circle c1 = new Circle(20);

calcArea( c1 );
}

public static void calcArea( Circle c )
{
System.out.println(" Area of Circle : " + c.area());
}
}

Output:

  Area of Circle : 314.0







11. Program to use exit() function

class ExitTest1
{
public static void main( String args[] )
{
System.out.println( "AAA" );
funTest();
System.out.println( "DDD" );
}

public static void funTest()
{
int a = 10;
System.out.println( "BBB" );
if( a == 10 )
System.exit(0);
System.out.println( "CCC" );
}
}

Output:

AAA
BBB





12. Program to use return statement.

class ReturnTest1
{
public static void main( String args[] )
{
System.out.println( "AAA" );
funTest();
System.out.println( "DDD" );
}

public static void funTest()
{
int a = 10;
System.out.println( "BBB" );
if( a == 10 )
return;
System.out.println( "CCC" );
}
}

Output:

AAA
BBB
DDD






13. Program to use instance of  operator

class ABC
{
ABC()
{
System.out.println( "Object is created." );
}
}

class XYZ
{
XYZ()
{
System.out.println( "Object is created." );
}
}

public class OpInstanceOf
{
public static void main( String args[ ] )
{
ABC a = new ABC();

if( a  instanceof  ABC )
{
System.out.println( "a is an instance of class ABC." );
}
else
{
System.out.println( "a is not an instance of class ABC." );
}

if( a  instanceof  XYZ )
{
System.out.println( "a is an instance of class XYZ." );
}
else
{
System.out.println( "a is not an instance of class XYZ." );
}
}
}

Output:

Object is created.
a is an instance of class ABC.
a is not an instance of class XYZ.







14. Program to use Simple class

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

class  BoxTest1
{
public static void main(String args[ ])
{
Box b1 = new Box();
double vol;

b1.width = 10;
b1.height = 15;
b1.depth = 20;

vol = b1.width * b1.height * b1.depth;
System.out.println("Volume is " + vol);
}
}

Output:

Volume is 3000.0





15. Program to show use of two objects

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

class  BoxTest2
{
public static void main(String args[ ])
{
Box  b1 = new Box();
Box  b2 = new Box();
double vol;

b1.width = 10;
b1.height = 15;
b1.depth = 20;

b2.width = 5;
b2.height = 6;
b2.depth = 7;

vol = b1.width * b1.height * b1.depth;
System.out.println("Volume is " + vol);

vol = b2.width * b2.height * b2.depth;
System.out.println("Volume is " + vol);
}
}

Output:

Volume is 3000.0
Volume is 210.0







16. Program to explain Function within class through Box class

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

void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}

class  BoxTest3
{
public static void main(String args[ ])
{
Box b1 = new Box();
Box b2 = new Box();
double vol;

b1.width = 10;
b1.height = 15;
b1.depth = 20;

b2.width = 5;
b2.height = 6;
b2.depth = 7;

b1.volume();
b2.volume();
}
}

Output:

Volume is 3000.0
Volume is 210.0







17. Program to explain Function with return value within class through Box class

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

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

class  BoxTest4
{
public static void main(String args[ ])
{
Box b1 = new Box();
Box b2 = new Box();
double vol;

b1.width = 10;
b1.height = 15;
b1.depth = 20;

b2.width = 5;
b2.height = 6;
b2.depth = 7;

vol = b1.volume();
System.out.println("Volume is " + vol);

vol = b2.volume();
System.out.println("Volume is " + vol);
}
}

Output:

Volume is 3000.0
Volume is 210.0

           
           
           
          

18. Program to initialize an object (private instance variable) using default constructor in Box class

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

void initBox(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}

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

class  BoxTest6
{
public static void main(String args[ ])
{
Box b1 = new Box();
Box b2 = new Box();

b1.initBox(10, 15, 20);

b2.initBox(5, 6, 7);

System.out.println("Volume is " + b1.volume());
System.out.println("Volume is " + b2.volume());
}
}

Output:

Volume is 3000.0
Volume is 210.0

           
           
           
           
           
           
           

19. Program to initialize an object (private instance variables) using default constructor in Box class

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

Box()
{
width = 10;
height = 10;
depth = 10;
}

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

class  BoxTest7
{
public static void main(String args[ ])
{
Box b1 = new Box();
Box b2 = new Box();

System.out.println("Volume is " + b1.volume());
System.out.println("Volume is " + b2.volume());
}
}

Output:

  Volume is 1000.0
  Volume is 1000.0

           
           
           
           
           
           

20. Program to initialize an object (private intance variables) using parametrise constructor in Box class

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

Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}

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

class  BoxTest8
{
public static void main(String args[ ])
{
Box b1 = new Box(10, 15, 20);
Box b2 = new Box(5, 6, 7);

System.out.println("Volume is " + b1.volume());
System.out.println("Volume is " + b2.volume());
}
}

Output:

Volume is 3000.0
Volume is 210.0

           
           
           
           
           
           
           

21. Program to initialize an object (using this to un hide instance variable) using parameterised constructor in Box class

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

Box(double width, double height, double depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}

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

class  BoxTest9
{
public static void main(String args[ ])
{
Box b1 = new Box(10, 15, 20);
Box b2 = new Box(5, 6, 7);

System.out.println("Volume is " + b1.volume());
System.out.println("Volume is " + b2.volume());
}
}

Output:

  Volume is 3000.0
  Volume is 210.0

           
           
           
           
           
           

22. Program to initialize objects using different types of constructor in Box class ( Constructor Overloading )

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

Box()
{
width = 1;
height = 1;
depth = 1;
}

Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}

Box(double len)
{
width = height = depth = len;
}

Box(Box b)
{
width = b.width;
height = b.height;
depth = b.depth;
}

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

class  BoxTest10
{
public static void main(String args[ ])
{
Box b1 = new Box();
Box b2 = new Box(10, 15, 20);
Box b3 = new Box(10);
Box b4 = new Box(b2);

System.out.println("Volume is " + b1.volume());
System.out.println("Volume is " + b2.volume());
System.out.println("Volume is " + b3.volume());
System.out.println("Volume is " + b4.volume());
}
}

Output:

Volume is 1.0
Volume is 3000.0
Volume is 1000.0
Volume is 3000.0

           
           
           
           
           
           
           

23.  Program to explain Passing Object to method

class  Circle
{
private double radius;

Circle()
{
radius = 0.0;
}

Circle( double radius)
{
this.radius = radius;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class  CirclePassingObject
{
public static void main( String args[ ] )
{
Circle c1 = new Circle(20);

calcArea( c1 );
}

public static void calcArea( Circle c )
{
System.out.println(" Area of Circle : " + c.area());
}
}

Output:

  Area of Circle : 314.0

           
           
           
           
           

24. Program to explain Returning Object from method

class  Circle
{
private double radius;

Circle()
{
radius = 0.0;
}

Circle( double radius)
{
this.radius = radius;
}

public double area()
{
return 3.14 * radius * radius;
}
}

public class CircleReturningObject
{
public static void main( String args[ ] )
{
Circle c1;

c1 = createCircle();
calcArea( c1 );
}

public static Circle createCircle()
{
Circle c = new Circle( 10 );
return  c;
}

public static void calcArea( Circle c )
{
System.out.println(" Area of Circle : " + c.area());
}
}

Output:

  Area of Circle : 314.0

           
           
           
           
           
           
           

25. Program to use Nested Class.

class Outer
{
private int a;

Outer()
{
a = 10;
}

public class Inner
{
private int b;

Inner()
{
b = 20;
}

void showInner()
{
System.out.println(" b = " + b);
}
}

void showOuter()
{
System.out.println(" a = " + a);
}
}

class NestedCls1
{
public static void main( String args[ ] )
{
Outer otr = new Outer();
Outer.Inner inr = otr.new Inner();

otr.showOuter();
inr.showInner();
}
}

Output:

  a = 10
  b = 20

           
           
           
           
           
           

26. Program to explain that private variable of Outer class can be accessible in Inner class but private variable of Inner class can not be  accessible in Outer class.

class Outer
{
private int a;

Outer()
{
a = 10;
}

public class Inner
{
private int b;

Inner()
{
b = 20;
}

void showInner()
{
System.out.println(" a = " + a);
System.out.println(" b = " + b);
}
}

void showOuter()
{
System.out.println(" a = " + a);
//  System.out.println(" b = " + b); // Error
}
}

class NestedCls2
{
public static void main( String args[ ] )
{
Outer otr = new Outer();
Outer.Inner inr = otr.new Inner();

otr.showOuter();
inr.showInner();
}
}

Output:

  a  =  10
  a  =  10
  b  =  20

           
           
           
           
           
           
           

27. Program to create Inner class Object in Outer class.

class Outer
{
private int a;

Outer()
{
a = 10;
}

public class Inner
{
private int b;

Inner()
{
b = 20;
}

void showInner()
{
System.out.println(" b = " + b);
}
}

void showOuter()
{
Inner inr = new Inner();
System.out.println(" a = " + a);
inr.showInner();
}
}

class NestedCls3
{
public static void main( String args[ ] )
{
Outer otr = new Outer();

otr.showOuter();
}
}

Output:

  a  =  10
  b  =  20

           
           
           
           
           
           
           

28. Program to explain Static Inner Class.

class Outer
{
private int a;

Outer()
{
a = 10;
}

public static class Inner
{
private int b;

Inner()
{
b = 20;
}

void showInner()
{
System.out.println(" b = " + b);
}
}

void showOuter()
{
System.out.println(" a = " + a);
}
}

class NestedCls4
{
public static void main( String args[ ] )
{
Outer otr = new Outer();
Outer.Inner inr = new Outer.Inner();
otr.showOuter();
inr.showInner();
}
}

Output:

  a  =  10
  b  =  20

           
           
           
           
           
           
     

 29. Program to explain that the Inner class can be defined within the method, loop, if-block or any other block.

class Outer
{
int outer_x = 100;
  
void test()
{
for(int i=0; i<5; i++)
{
class Inner
{
void display()
{
System.out.println("outer_x = " + outer_x);
}
}
Inner inr = new Inner();
inr.display();
}
}
}
  
class NestedCls20
{
public static void main(String args[ ])
{
Outer otr = new Outer();
otr.test();
}
}

Output:

outer_x = 100
outer_x = 100
outer_x = 100
outer_x = 100
outer_x = 100

           
           
           
           
           
           
           

30. Program to use Factory method to create Inner class Objects.

class Outer
{
class Inner
{
private int x = 10;

public int getX()
{
return x;
}
}

public  Inner  createInner()
{
return new Inner();
}

public void show()
{
Inner  inr = createInner();

System.out.println(" x : " + inr.getX());
}
}

class NestedCls9
{
public static void main(String[] args)
{
Outer otr = new Outer();
otr.show();
}
}

Output:

  x : 10
                              


Reactions

Post a Comment

0 Comments