Introduction
The constructor plays significant role in the nay kind of programming language. Here I explained the types of constructor and its limitation with suitable examples. Let us see the constructor.
Constructors
The constructor is a special method to the class and struts. This constructor is used to initialize the types and objects. The complier consider as constructor when the constructor have the same name as class name. Every class has the default constructor in the class (default constructor). Which can be called as implicit constructor. This can be used to define the default values the data types when there are not defined.
The user can use the default constructor for explicitly to create the instance of the classes. The constructor access modifier is public then it will be executed when the object is instantiated. The private constructor cannot be declaring. When object instantiate the outside class then it will throw an error like protection level. The internal also can be defined access modifier to the class constructor that will support up to that current assembly.
Limitations
- The constructor should have the same as the class or struct name.
- It cannot return any values to the calling program or function.
- It static constructor cannot have any parameters.
- It should have public or internal access modifiers. It can be declared as private but no use of it. When we instantiate the object it must throw compilation error like inaccessible due to protection level.
- It can be used to capture the parameters and initialize the values rather than do the complex logics.
Constructor
The simple constructor can be used to instantiate the object and define the data type values.
The syntax of the constructor is the following
access-modifier Class_Name(data type Parameter1,data type Parameter 2, [Classes Obj])
{
//Code here
}
using System;
public class SimpCons
{
private int a;
private int b;
public SimpCons(int x, int y)
{
a = x;
b = y;
Display();
}
public void Display()
{
Console.WriteLine("The value of a is " + a);
Console.WriteLine("The value of b is " + b);
}
}
class SimpleConstructor
{
public static void Main()
{
SimpCons oSimpCons = new SimpCons(5, 5);
}
}
In the above code, the constructor is used to initialize the values to the variables.
The constructor can be called the user defined the method in the both base and inherited class. Suppose I have not used the constructor then it will use the implicit constructor to initialize the values for the variables.
Overload Constructor
The constructor can be overloaded. As you know the constructor should be same as class name. But it can be overloaded just like method overloading. The same polymorphism concept can be applicable for the constructor. The same method name with different arguments.
Overload Constructor with different data types
The constructor can be overloaded with the same name of the constructor with different signature. The compiler only considers the signature type of the constructor. The arguments may be like int, string, char, float, double, etc..,
using System;
class OverCons
{
public OverCons()
{
Console.WriteLine("Hi! Welcome to Overload Constructor");
Console.WriteLine("-----------------------------------");
}
public OverCons(int a, int b)
{
Console.WriteLine("The addition of " + a + " and " + b + " is " + (a + b));
}
public OverCons(string str1, string str2)
{
Console.WriteLine("Your full name is " + (str1 + str2));
}
}
class ConstuctorOverload
{
public static void Main(string[] args)
{
OverCons oOverCons = new OverCons();
OverCons oOverCons1 = new OverCons(90, 10);
OverCons oOverCons2 = new OverCons("Senthil", "kumar");
}
}
This example shows there are three overloaded constructors. One is default constructor, second one is it accepts two integer values. Final constructor accepts the string. It can be any different combination of data types of parameter to the functions. It will not allow the constructor with the same signature types with different variable name. Because the compiler identifies the constructor based on the type of signature matches.
Overload Constructor with different class objects
When overload the constructors it accepts the different types of class objects. It can be overloaded with the objects.
using System;
using System.Collections;
using System.Text;
class ObjOver
{
public ObjOver(ArrayList oArrayList)
{
Console.WriteLine("Constructor - ArrayList");
Console.WriteLine("-----------------------");
for (int i = 0; i < oArrayList.Count; i++)
{
Console.WriteLine(oArrayList[i].ToString());
}
}
public ObjOver(Stack oStack)
{
Console.WriteLine("Constructor - Stack");
Console.WriteLine("-------------------");
foreach (string str in oStack)
{
Console.WriteLine(str);
}
}
public ObjOver(StringBuilder oSB)
{
Console.WriteLine("Constructor - StringBuilder");
Console.WriteLine("---------------------------");
Console.WriteLine(oSB.ToString());
}
}
class ObjOverCons
{
public static void Main(string[] args)
{
ArrayList oArrayList = new ArrayList();
for (int i = 0; i < 10; i++)
{
oArrayList.Add(i);
}
Stack oStack = new Stack();
oStack.Push("Senthil");
oStack.Push("Kumar");
StringBuilder oSB = new StringBuilder();
oSB.Append("Welcome To Object Overload Constructor");
oSB.Append(Environment.NewLine);
oSB.Append("Sample programs developed by Erode Senthilkumar");
ObjOver oObjOver1 = new ObjOver(oArrayList);
ObjOver oObjOver2 = new ObjOver(oStack);
ObjOver oObjOver3 = new ObjOver(oSB);
}
}
Here the constructor has overloaded with the class objects.
Private Constructor
The private constructor is a special method in the class. It can be only defined when the class has only the static members.
using System;
public class StaticBase
{
private StaticBase()
{
Console.WriteLine("Hi");
}
public static void Display()
{
Console.WriteLine("Display() method");
}
}
class PrivateStatic
{
public static void Main(string[] args)
{
StaticBase.Display();
}
}
PallaviPosted Nov 5, 2012, 9:34 PM
thank you.
Abhijeet SinhaPosted May 11, 2010, 1:57 AM
Please let me know that is there any use of virtual constructor.If then how can we use it.