Constructor Overloading

Author: Ravi Poswal

If a class provides multiple implementations of a constructor or method, the method or constructor is said to be overloaded. Overloading is the subset of polymorphism. In constructor overloading, we follow some below rules like that:-

  • Different number of parameters

  • Different types of parameters

Code Example

class A
{
  A()
   { 
            System.out.println("default constructor");  
  }
A(int x)
  {    //you can direct put the parameter
     System.out.println("parameterized constructor "+x);
  }
A(double w)
{ 
double t;
t=w;   // you can put parameters using variable
 System.out.println("parameterized constructor "+t);
   } 
public static void main(String arg[])
{
A d=new A();
A e=new A(34);
A f=new A(23.4);
}
}