Program to Implement Constructor Overloading in Java

Program to Implement Constructor Overloading in Java


     This program is used to implement constructor overloading. Here the integer, float, double variable and object is passed as arguments.
 
import java.io.*;
import java.util.*;
class Box1
{
            int wt,ht,dt,a;
            float wt1,ht1,dt1,a2;
            double wt2,ht2,dt2,a3;
            Box1()

            {
                        wt=10;
                        ht=10;
                        dt=10;
                        a=wt*ht*dt;
                        System.out.println("The volume of box is(No Arugument) "+a);
            }
            Box1(int i)
            {
                        wt=i;
                        ht=15;
                        dt=20;
                        a=wt*ht*dt;
                        System.out.println("The volume of box is(One Argument) "+a);
            }
            Box1(int a,int b,int c)
            {
                        wt=a;
                        ht=b;
                        dt=c;
                        a=wt*ht*dt;
                        System.out.println("The volume of box is(Multi Argument Integer) "+a);
            }
            Box1(float x,float y,float z)
            {
                        wt1=x;
                        ht1=y;
                        dt1=z;
                        a2=wt1*ht1*dt1;
                        System.out.println("The volume of box is(Multi Argument Float) "+a2);
            }
            Box1(double d,double f,double e)
            {
                        wt2=d;
                        ht2=f;
                        dt2=e;
                        a3=wt2*ht2*dt2;
                        System.out.println("The volume of box is(Multi Argument Double) "+a3);
            }
            Box1(Box1 a1)
            {
                        wt=a1.wt;
                        ht=a1.ht;
                        dt=a1.dt;
                        a=wt*ht*dt;
                        System.out.println("The volume of box is(Object as Argument) "+a);
            }
}
class Box
{
            public static void main(String args[])
            {
                        Box1 a=new Box1();
                        Box1 b=new Box1(10);
                        Box1 c=new Box1(10,20,30);
                        Box1 d=new Box1(15.5f,20.0f,31.5f);
                        Box1 e=new Box1(2314,5676,7654);
                        Box1 f=new Box1(c);
            }
}         

Output

The volume of box is(No Arugument) 1000
The volume of box is(One Argument) 3000
The volume of box is(Multi Argument Integer) 6000
The volume of box is(Multi Argument Float) 9765.0
The volume of box is(Multi Argument Integer) 1745408848
The volume of box is(Object as Argument) 6000

No comments:

Post a Comment

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...