Page 306 - CA_Blue( J )_Class10
P. 306

12.3 CONSTRUCTOR OVERLOADING
              Constructor overloading is same as method overloading. It is a technique of using a number of constructors in a class
              with different number of parameters or different types of parameters. Let us take the following example:


                 1 class shape
                 2 {

                 3     int side, length, breadth;
                 4     double radius;

                 5     shape()
                 6     {

                 7         side=0;
                 8         length=0; breadth=0;

                 9         radius=0.0;
                10          System.out.println("All instance variables are initialized with its"
                    +"default values");

                11         System.out.println("Side : " + side);
                12         System.out.println("Length : " + length + "breadth : "+ breadth);
                13         System.out.println("Radius : " + radius);

                14         System.out.println("---------------------------------------------");    }

                15

                16     shape(int s)
                17     {

                18         side = s;
                19         System.out.println("It is a square with area :" + (side * side));

                20         System.out.println("Length : " + length + "breadth : "+ breadth);
                21         System.out.println("Radius : " + radius);

                22         System.out.println("--------------------------------------------");

                23     }
                24     shape(int l, int b)

                25     {
                26         length = l;

                27         breadth = b;
                28         System.out.println("It is a rectangle with area :" + (length * breadth));

                29         System.out.println("Side : " + side);
                30         System.out.println("Radius : " + radius);

                31         System.out.println("------------------------------------------------");


                304304  Touchpad Computer Applications-X
   301   302   303   304   305   306   307   308   309   310   311