C# | Constructor Overloading
It is quite similar to the Method Overloading. It is the ability to redefine a Constructor in more than one form. A user can implement constructor overloading by defining two or more constructors in a class sharing the same name. C# can distinguish the constructors with different signatures. i.e. the constructor must have the same name but with different parameters list. We can overload constructors in different ways as follows:
- By using different type of arguments
- By using different number of arguments
- By using different order of arguments
By changing the Data types of the parameters
Example:
public ADD (int a, float b);
public ADD (string a, int b);
Here the name of the class is ADD. In first constructor there are two parameters, first one is int and another one is float and in second constructor, also there is two parameters, first one is string type and another one is int type. Here the constructors have the same name but the types of the parameters are different, similar to the concept of method overloading.
CSharp
using System;
class ADD {
int x, y;
double f;
string s;
public ADD( int a, double b)
{
x = a;
f = b;
}
public ADD( int a, string b)
{
y = a;
s = b;
}
public void show()
{
Console.WriteLine("1st constructor ( int + float ): {0} ",
(x + f));
}
public void show1()
{
Console.WriteLine("2nd constructor ( int + string ): {0}",
(s + y));
}
}
class GFG {
static void Main()
{
ADD g = new ADD(10, 20.2);
g.show();
ADD q = new ADD(10, "Roll No. is ");
q.show1();
}
}
|
Output:
1st constructor (int + float): 30.2
2nd constructor (int + string): Roll No. is 10
By changing the number of the parameters
In this case, we will use two or more constructors having the different number of parameters. The data types of arguments can be the same but the number of parameters will be different. Example:
public ADD (int a, int b);
public ADD (int a, int b, int c);
Here, the class name is ADD. In the first constructor the number of parameter is two and the types of the parameters is int. In second constructor the number of parameter is three and the types of the parameters are also int, there is no problem with the data types.
CSharp
using System;
class ADD {
int x, y;
int f, p, s;
public ADD( int a, int b)
{
x = a;
y = b;
}
public ADD( int a, int b, int c)
{
f = a;
p = b;
s = c;
}
public void show()
{
Console.WriteLine("1st constructor ( int + int ): {0} ",
(x + y));
}
public void show1()
{
Console.WriteLine("2nd constructor ( int + int + int ): {0}",
(f + p + s));
}
}
class GFG {
static void Main()
{
ADD g = new ADD(10, 20);
g.show();
ADD q = new ADD(10, 20, 30);
q.show1();
}
}
|
Output:
1st constructor (int + int): 30
2nd constructor (int + int + int): 60
By changing the Order of the parameters
Example:
public student(double a, int x, string s)
public student(string s, int x, double a)
Here, the two constructor hold the same types of parameters, that is, each constructor has one double type, one int type and one string type parameter, but the positions of the parameters are different. The compiler will invoke the constructor according to their argument order.
CSharp
using System;
class student {
public int roll;
public double Height;
public string name;
public student( double a, int x, string s)
{
roll = x;
name = s;
Height = a;
}
public student( string s, int x, double a)
{
Height = a;
roll = x;
name = s;
}
public void show()
{
Console.WriteLine("Roll Number: " + roll);
Console.WriteLine("Height: " + Height + "feet");
Console.WriteLine("Name: " + name);
}
}
class Geeks {
static void Main()
{
student s1 = new student(5.7, 10, "Jhon Peterson");
student s2 = new student("Peter Perker", 11, 6.0);
Console.WriteLine("First Constructor: ");
s1.show();
Console.WriteLine();
Console.WriteLine("Second Constructor: ");
s2.show();
}
}
|
Output:
First Constructor:
Roll Number: 10
Height: 5.7feet
Name: Jhon Peterson
Second Constructor:
Roll Number: 11
Height: 6feet
Name: Peter Perker
Invoke an Overloaded Constructor using “this” keyword
We can call an overloaded constructor from another constructor using this keyword but the constructor must be belong to the same class, because this keyword is pointing the members of same class in which this is used. This type of calling the overloaded constructor also termed as Constructor Chaining. Example:
Let the class name is gfg,
Now
public gfg()
public gfg(int a) : this()
public gfg(double b) : this(int)
Here the first constructor is default constructor, second and third constructor are parameterized Constructor, where one has int type and another one has double type parameter. In second constructor, this() invoke the first constructor which is default constructor. Here, after this keyword there is only ( ) which means the compiler invoke constructor that has no argument, means default constructor. In third constructor, this(int) invoke the second constructor which is parameterized constructor. Here, after this there is (int) which means the compiler invoke constructor that has int type argument.
CSharp
using System;
class GFG {
private int Length, Height;
private double Width;
public GFG()
{
Console.WriteLine("Default Constructor Invoked");
}
public GFG( int l, double w) : this ()
{
Console.WriteLine("Parameterized Constructor in 2nd Constructor");
Length = l;
Width = w;
}
public GFG( int l, double w, int h) : this (l, w)
{
Console.WriteLine("Parameterized Constructor in 3rd Constructor");
Height = h;
}
public double Volume()
{
return (Length * Width * Height);
}
}
class Geeks {
public static void Main()
{
GFG g = new GFG(10, 20.5, 30);
Console.WriteLine("Volume is : {0}", g.Volume());
}
}
|
Output:
Default Constructor Invoked
Parameterized Constructor in 2nd Constructor
Parameterized Constructor in 3rd Constructor
Volume is : 6150
Overloading of Copy Constructor
A parameterized constructor that contains a parameter of same class type is called a copy constructor. Basically, copy constructor is a constructor which copies a data of one object into another object. Its main use is to initialize a new instance to the values of an existing instance.
CSharp
using System;
class GFG {
public string p1, p2;
public int p3, p4;
public GFG( string x, string y)
{
p1 = x;
p2 = y;
}
public GFG(GFG gf)
{
p1 = gf.p1;
p2 = gf.p2;
}
public GFG( int a, int b)
{
p3 = a;
p4 = b;
}
public GFG(GFG a, GFG b)
{
p3 = a.p3;
p4 = b.p4;
}
}
class Geeks {
static void Main()
{
GFG g = new GFG("Welcome", "GeeksForGeeks");
GFG g1 = new GFG(g);
Console.WriteLine(g1.p1 + " to " + g1.p2);
GFG G = new GFG(10, 20);
GFG G1 = new GFG(G, G);
Console.WriteLine("Overloaded values : {0} and {1}",
G1.p3, G1.p4);
}
}
|
Output:
Welcome to GeeksForGeeks
Overloaded values : 10 and 20
Note:
- Static Constructor cannot be overload, because Static Constructors are parameterless constructor, but for overloading, we must need parameterized constructor.
- Private Constructor can be overloaded and we can use that by instance of this class inside the same class. Private members cannot be accessed from outside the class.
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...