Following code will explain the overloading of constructor in java::
There are three types of constructors:
1. Default(No argument)
2. Parametrized
3. Copy
Code ::
import java.util.*;
class overload
{
int a;
Scanner in=new Scanner(System.in);
public overload()
{
System.out.println("Enter Any Value");
a=in.nextInt();
}
public overload(int c)
{
a=c;
}
public overload(overload c)
{
a=c.a;
}
void show()
{
System.out.println("Value is "+a);
}
}
public class Constructor
{
public static void main(String[] nt)
{
overload ob=new overload(); //default constructor
overload ob2=new overload(76); //parametrized constructor
overload ob3=new overload(ob); //copy constructor
ob.show();
ob2.show();
ob3.show();
}
}
Output:
Special Thanks to Mr.Nikhil Chauhan For This Program....
There are three types of constructors:
1. Default(No argument)
2. Parametrized
3. Copy
Code ::
import java.util.*;
class overload
{
int a;
Scanner in=new Scanner(System.in);
public overload()
{
System.out.println("Enter Any Value");
a=in.nextInt();
}
public overload(int c)
{
a=c;
}
public overload(overload c)
{
a=c.a;
}
void show()
{
System.out.println("Value is "+a);
}
}
public class Constructor
{
public static void main(String[] nt)
{
overload ob=new overload(); //default constructor
overload ob2=new overload(76); //parametrized constructor
overload ob3=new overload(ob); //copy constructor
ob.show();
ob2.show();
ob3.show();
}
}
Output:
Special Thanks to Mr.Nikhil Chauhan For This Program....