Recent Posts

header ads

Constructor overloading in java with examples । KpTechSolution

➧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 -
Constructor overloading in java in hindi-जानें हिन्दी में-Constructor overloading programs in java
Constructor Overloading in Java
  1. MyCon( int a, int b )   // constructor
    {
    int sum = a + b;
    return sum ;
    }

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 -


  1. MyCon obj = new MyCon( 5, 10 ) ;

Now suppose that we want to print a number of Square, for that we create Constructor as follows -


  1. MyCon ( int a )
    {
    int  square = a * a ;
    return square ;
    }

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 :


  1. MyCon obj = new MyCon ( 10 ) ;

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 :
  1. Class Mycon
    {
    int a, b ;
    MyCon ( int x, int y)
    {
    a = x ;
    b = y ;
    int result = a + b ;
    System.out.println ( “ Sum is : “+result );
    }
    MyCon ( int x )
    {
    a = x ;
    int square = a * a ;
    System.out.println ( “ Square is : “+square );
    }
    public static void main ( String args [] )
    {
    MyCon o = new MyCon ( 5, 10 );
    MyCon o = new MyCon ( 5 );
    }
    }
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 :

  1. Class Mycon
    {
    int a ;
    float b;
    MyCon ( int x, float y)
    {
    a = x ;
    b = y ;
    int result = a + b ;
    System.out.println (“ Sum is : “+result);
    }
    MyCon ( int x )
    {
    a = x ;
    int square = a * a ;
    System.out.println (“ Square is : “+square);
    }
    public static void main ( String args [] )
    {
    MyCon o = new MyCon ( 5, 11.5 );
    MyCon o = new MyCon ( 4 );
    }
    }
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 

Post a Comment

0 Comments