➧Constructor overloading :
Constructor overloading is a concept in java language, in which we make multiple constructors inside a class, in its parameters, we pass the parameters of different Datatype. This is called Constructor. For example, suppose we make Constructor to sum any two numbers, we can create this as follows -
Now assume that we have to sum 5 and 10. So when we create an Object for that, the numbers which pass the code, we write as follows -
Now suppose that we want to print a number of Square, for that we create Constructor as follows -
Let us suppose that we have to print a square of 10, then when we create an Object for that, the parameter in the Parameter will be written as follows :
Now, these two constructors will be created inside a class, then this will be called Constructor Overloading. Now the question is why is Constructor Overloading in it? So you should be seeing in both the Constructors that two Parameters have been passed in one and the Single Parameter has been passed in the other. Two different objects will be created for both of these Constructors and the different Parameters will be passed for different Constructors in both. Constructor understands the overloading concept through Java's Source Program.
Source Code :
OUTPUT : Sum is : 15
Square is : 25
Read Also :
- Constructor in Java - What is Constructor with Examples.
Another concept in constructor overloading is that multiple constructor is created within a class and variables of different Datatype are passed in their parameters. And when you create an object, the value of the same Data-type types is passed or else the error in the program is generated. We also understand this concept through a Java Source Program.
Source Code :
OUTPUT : Sum is : 16.5
Square is : 16
Read Also :
- Constructor in Java - What is Constructor with Examples.
Like this post, If you find Topic useful in this post, so share it with your friends and colleagues on Facebook, Twitter, whatsapp and Google Plus.
Thank You !
Note – For More Core & Advance Java Concepts , Tutorials And Programs Are Available in www.kptechsolution.com
0 Comments