Advertisement
LegendSujay2019

Constructor Overloading in Java

Aug 31st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. Constructor Overloading in Java
  2.  
  3. In addition to overloading methods, we can also overload constructors in java. Overloaded constructor is called based upon the parameters specified when new is executed.
  4.  
  5. When do we need Constructor Overloading?
  6.  
  7. Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. For example, Thread class has 8 types of constructors. If we do not want to specify anything about a thread then we can simply use default constructor of Thread class, however if we need to specify thread name, then we may call the parameterized constructor of Thread class with a String args like this:
  8.  
  9. Thread t= new Thread (" MyThread ");
  10. Let us take an example to understand need of constructor overloading. Consider the following implementation of a class Box with only one constructor taking three arguments.
  11.  
  12. // An example class to understand need of
  13. // constructor overloading.
  14. class Box
  15. {
  16.     double width, height,depth;
  17.  
  18.     // constructor used when all dimensions
  19.     // specified
  20.     Box(double w, double h, double d)
  21.     {
  22.         width = w;
  23.         height = h;
  24.         depth = d;
  25.     }
  26.  
  27.     // compute and return volume
  28.     double volume()
  29.     {
  30.         return width * height * depth;
  31.     }
  32. }
  33. As we can see that the Box() constructor requires three parameters. This means that all declarations of Box objects must pass three arguments to the Box() constructor. For example, the following statement is currently invalid:
  34.  
  35. Box ob = new Box();
  36. Since Box() requires three arguments, it’s an error to call it without them. Suppose we simply wanted a box object without initial dimension, or want to to initialize a cube by specifying only one value that would be used for all three dimensions. From the above implementation of Box class these options are not available to us.
  37.  
  38. These types of problems of different ways of initializing an object can be solved by constructor overloading. Below is the improved version of class Box with constructor overloading.
  39.  
  40. filter_none
  41. edit
  42. play_arrow
  43.  
  44. brightness_4
  45. // Java program to illustrate
  46. // Constructor Overloading
  47. class Box
  48. {
  49.     double width, height, depth;
  50.  
  51.     // constructor used when all dimensions
  52.     // specified
  53.     Box(double w, double h, double d)
  54.     {
  55.         width = w;
  56.         height = h;
  57.         depth = d;
  58.     }
  59.  
  60.     // constructor used when no dimensions
  61.     // specified
  62.     Box()
  63.     {
  64.         width = height = depth = 0;
  65.     }
  66.  
  67.     // constructor used when cube is created
  68.     Box(double len)
  69.     {
  70.         width = height = depth = len;
  71.     }
  72.  
  73.     // compute and return volume
  74.     double volume()
  75.     {
  76.         return width * height * depth;
  77.     }
  78. }
  79.  
  80. // Driver code
  81. public class Test
  82. {
  83.     public static void main(String args[])
  84.     {
  85.         // create boxes using the various
  86.         // constructors
  87.         Box mybox1 = new Box(10, 20, 15);
  88.         Box mybox2 = new Box();
  89.         Box mycube = new Box(7);
  90.  
  91.         double vol;
  92.  
  93.         // get volume of first box
  94.         vol = mybox1.volume();
  95.         System.out.println(" Volume of mybox1 is " + vol);
  96.  
  97.         // get volume of second box
  98.         vol = mybox2.volume();
  99.         System.out.println(" Volume of mybox2 is " + vol);
  100.  
  101.         // get volume of cube
  102.         vol = mycube.volume();
  103.         System.out.println(" Volume of mycube is " + vol);
  104.     }
  105. }
  106. Output:
  107.  
  108. Volume of mybox1 is 3000.0
  109. Volume of mybox2 is 0.0
  110. Volume of mycube is 343.0
  111. Using this() in constructor overloading
  112.  
  113. this() reference can be used during constructor overloading to call default constructor implicitly from parameterized constructor. Please note, this() should be the first statement inside a constructor.
  114.  
  115. filter_none
  116. edit
  117. play_arrow
  118.  
  119. brightness_4
  120. // Java program to illustrate role of this() in
  121. // Constructor Overloading
  122. class Box
  123. {
  124.     double width, height, depth;
  125.     int boxNo;
  126.  
  127.     // constructor used when all dimensions and
  128.     // boxNo specified
  129.     Box(double w, double h, double d, int num)
  130.     {
  131.         width = w;
  132.         height = h;
  133.         depth = d;
  134.         boxNo = num;
  135.     }
  136.  
  137.     // constructor used when no dimensions specified
  138.     Box()
  139.     {
  140.         // an empty box
  141.         width = height = depth = 0;
  142.     }
  143.  
  144.     // constructor used when only boxNo specified
  145.     Box(int num)
  146.     {
  147.         // this() is used for calling the default
  148.         // constructor from parameterized constructor
  149.         this();
  150.  
  151.         boxNo = num;
  152.     }
  153.  
  154.     public static void main(String[] args)
  155.     {
  156.         // create box using only boxNo
  157.         Box box1 = new Box(1);
  158.  
  159.         // getting initial width of box1
  160.         System.out.println(box1.width);
  161.     }
  162. }
  163. Output:
  164.  
  165. 0.0
  166. As we can see in the above program that we called Box(int num) constructor during object creation using only box number. By using this() statement inside it, the default constructor(Box()) is implicitly called from it which will initialize dimension of Box with -1.
  167.  
  168. Note : The constructor calling should be first statement in the constructor body. For example, following fragment is invalid and throws compile time error.
  169.  
  170. Box(int num)
  171. {
  172.     boxNo = num;
  173.  
  174.     /* Constructor call must be the first
  175.        statement in a constructor */
  176.     this();  /*ERROR*/
  177. }
  178. Important points to be taken care while doing Constructor Overloading :
  179.  
  180. Constructor calling must be the first statement of constructor in Java.
  181. If we have defined any parameterized constructor, then compiler will not create default constructor. and vice versa if we don’t define any constructor, the compiler creates the default constructor(also known as no-arg constructor) by default during compilation
  182. Recursive constructor calling is invalid in java.
  183. Constructors overloading vs Method overloading
  184.  
  185. Strictly speaking, constructor overloading is somewhat similar to method overloading. If we want to have different ways of initializing an object using different number of parameters, then we must do constructor overloading as we do method overloading when we want different definitions of a method based on different parameters.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement