Showing posts with label Constructor. Show all posts
Showing posts with label Constructor. Show all posts

Monday, January 17, 2011

How To call Constructor from another Constructor

Calling Constructor From Another Constructor
We can also call constructor from another constructor.


Example




We have use this() after every constructor which mean that before executing the constructor go to this() constructor. Human : this("Hello"). It means that before executing Human default constructor compiler will execute Human constructor which takes string as a argument (i.e. Human(string a).




Constructor Chaining

Constructor Chaining
You can achieve constructor chaining through inheritance. Constructor chaining is a process in which the first task of derived constructor is to call its base class constructor. There can be a number of classes in an inheritance chain. In constructor chaining the derive class will call its base class and this process will continue until it reaches its top most class and then all constructor will execute as a chain from top till original class.

Example
Let make a program to understand the mechanism of constructor chaining.


We have create three class. Stud, Male and Human. Stud is inherited from Male and Male is inherited from Human. Human is at the top and Stud is in bottom.



We have create an object of Stud class. What will happen if we create Stud object. Compiler will go to Stud constructor and before executing the Stud constructor it will go to its base class  (i.e. Male). Before executing Male constructor it will go to its base class (i.e.Human). Now complier have reached at the top and will start executing base constructor (i.e Human) then its (Human) derived class constructor (i.e. Male) then its (Male) derived class constructor (i.e Stud).


Constructor Overloading

Constructor Overloading
Overloading of constructor is same as overloading of method. Constructor overloading is having two or more constructor within a same class with different parameters.
We can achieve overloading by
  • Number of Parameters
  • Types of Parameters
  • Arrangement of Parameters


Example
Number of Parameters


Types of Parameters


Arrangement of Parameters


Private Constructor

Private Constructor
You can also use make your constructor private. If constructor is private so it restricts the object to instantiate using new keyword. You can’t create object of private constructor but you can use access static members of class by calling them from class name. Private constructors are used when you don’t want to create an object of class or you don’t want the class to be inherited.


Example



In this program we have create a private constructor and variable Name as static. First we will look that what happen if we instantiate an object of private constructor.




Can't instantiate an object of private constructor. You can see that it says that you can't access the object Human() because of its protection level.


Now without instantiating object we will use static variable.






We have to call static variable with class name as we have done here Human.Name.






Now we will make another class and will inherit the Human class to see that the class contain private constructor can be inherit or not.




It gives the same error when we were creating object of private constructor.


Read about Constructor, Constructor Overloading, Constructor Chaining

Sunday, January 16, 2011

Constructor

Constructor
Constructor is a special method without any return type and have same name as class name. Constructor is used to initialize an object of a class and it runs or executes automatically when you create an object. Constructors are called only once per creation of an object while regular methods can be called many times. Constructor can be private, public, static and protected. We can also overload constructor.

In c# constructor are called using new keyword.
Car objcar = new Car();
When you use new keyword then object is created and runtime will construct that object by using the definition of the class. Then it grab a piece of memory and fills with the fields (variable or methods) defined by the class and then a constructor is invoked to perform any initialization required.

Constructors are generally of following types
  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

Default Constructor
When you create class without any constructor then compiler will provide you parameterless constructor with no body that parameterless constructor is called default constructor.

To explain default constructor we will make two program, in first program we will use default constructor which is provided by compiler and secondly we will create our own default constructor . 

We can't see default Constructor provided by compiler (but it is present in the class) thats why we will make our own default constructor to show that both are same.

First Program

You can see that we have two variables in class Name and LastName but not constructor but when we create an object of class its working properly. That means there is a default constructor in class provided by compiler but we can't see it.

Second Program

In this program we have two variables in class Name, LastName and a default constructor. When we will create an object the result will be the same as first program.
  
Parameterized Constructor
Constructor that accepts arguments when object is created is called parameterized constructor. Parameterized constructor is not provided by compiler you have to create this constructor. You can use parameterized constructor when you have to initialize data members of a class when object is created.


You can see Human constructor with two parameters Name and Lname. When we create an object we have to provide parameters (e.g. Human objH = new Human("James","Bond");) then Name = James and LastName = Bond will be assigned.

Copy Constructor
Copying values of one object to another object is called copy constructor. If you want to copy values of existing object to another new created object then you have to write method yourself because c# does not provide copy constructor.



We have create two object objH and objAnotherHuman, and will copy data of objH to objAnotherHuman. First we have create objH object and pass parameter(James, Bond) . It means that objH have Name = James and LastName = bond. Now we will create another object objAnotherHuman and will pass objH as parameter . Now copy constructor will copy obj.Name(i.e James) to objAnotherHuman.Name and obj.LastName(i.e Bond) to objAnotherHuman.

If you do not understand what happens or how values are assign in copy constructor then run the program in debugging mode 


Read about Private Constructor, Constructor Overloading, Constructor Chaining

How To call Constructor from another Constructor