Constructor is a method of a class which gets automatically executed when an object of that class is created or when the class is initlized.
--------------------------------------------------------------------------------------
Types of Constructor
A difficult Question, it can be categorized in many ways.
According to one way: There are two types of Constuctor, which is given as follows:
Default Constructor
A constructor that takes no parameters is called a default constructor. It automatically get called when a object of the class is created with no arguments.
Parameterized Constructor
This one is almost same as default constructor, the only thing is, it takes parameters.
According to second way: There are also two types of Constructor, which is given as follows:
Static Constructor/Class Constructor
It cannot be overloaded.
It takes no parameter and can access static members only
It executes only once for a class.
It should not be declared with any access modifier.
It is called automatically,and by no way its execution can be stopped.
It executes before any of the static member of the class is referenced
Instance Constructor/Non Static Constructor/type constructor. /type initilizers
They can be public, private, protected, external, or internal.
A private constructor prevents the creation of the object of the class (except in nested classes)and also prevent the Inheritance.
A class which contains internal constructor,object of this class can not be created in different assembly.
A class which contains protected constructor,object of this class can not be created directly,instead we need to create the object of the child class which in turn invoke the base class protected construcor.
One Important point is that a public constructor can access a private constructor of the same class by constructor chaining.
Example 1:-
class MyClass
{
public MyClass(): this("Hi")
{
Console.WriteLine("Default Constructor");
}
private MyClass(string val)
{
Console.WriteLine("Parameterized Constructor");
}
public static void Main(string []args)
{
MyClass Obj= new MyClass();
Console.ReadLine ();
}
}
The above program show how constructor chaining is implemented. In the above prog, default constructor first invoke the parameterized Constructor and then invoke itself.
Output Will be :-
Parameterized Constructor
Default Constructor
Example 2:-
Protected Constructor Can be called by its child class only, they can not be invoked directly by creating the object of that class. Following example demonstrate it:
class MyBase
{
protected MyBase(string s)
{
Console.WriteLine("Called By MyChild");
}
}
class MyChild: MyBase
{
public MyChild() : base ("Hello Father") {}
}
class MainClass
{
public static void Main(string []args)
{
MyChild Obj = new MyChild( ); // Fine
MyBase Obj1 = new MyBase("Hello Father"); //Error-- Can Be Invoke by Child Class Only
}
}
--------------------------------------------------------------------------------------
Points To Remember
Its name must be same as the name of class
It does not have return type ,not even void.
They can’t be inherited, although a derived class can class the base class constructor.
Declaring more than one constructors in a class with different sets of parameters known as Constructor overloading.
A constructor can call another constructor using :this() .
They can not be "virtual"
They are called in the order of inheritance(Exception is static Constructor)
--------------------------------------------------------------------------------------Constuctor Overloading
We can have more than One constructor in a class with different. these constructors are called Overloaded constructors. They must differ in their number of arguments, type of arguments,order of arguments.
--------------------------------------------------------------------------------------
Constructor Chaining
The concept which allows one constructor to invoke another constructor is called Constructor Chaining . for this purpose we either use :base (parameters) or : this (parameters) just before the definition of the constructor.
--------------------------------------------------------------------------------------
Note:
When a chidlls class object is created,i.e. its default constructor ,It first calls the default constructor Of base class ,then execute itself. Similarly whenever a paramerized constructor Of child need to invoke by it’s object ,by default it calls the base class default constructor again.
By Doin like this " public ClassName (string strName) : base(strName) "will call the base class paramerized constructor.
--------------------------------------------------------------------------------------
Thanks & Regards
Bheeshma P. Nayak
3 comments:
nice to find such complete and good article about constructor. it helped me learn about constructor in more refined way.
waiting for your other post on custom control and user control.......
Rejesh..
It is Live Now....
Post a Comment