Saturday 7 January 2012

Article 2:-Constructor in more Detail


For Asp.net Training with C# Click Here

Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com


In the previous article Click here for previous article we have learnt the class and constructor in the basic level. In this article we will learn more about the constructors and will study constructor in detail.

Ques:-1 what is the difference between method and constructors?
Ans:- a) Constructor doesn’t have any return type not even void but every method must contain the return type and if any method doesn’t return anything then it will return void.
b) Constructor called automatically but method is called by the object or with the class name
Note:-in case of static method we call method with the name of class.
c) Constructer will be called only once but we can call the method many times.

Ques 2:-What is the type of constructors?
Ans:- there are two types of constructor and these are following
a)      Static Constructor(also known as class constructor)
b)      Non static Constructor(also known as instance constructor)

Static Constructor(also known as class constructor)
Static constructor is that constructor which is called before the object creation of class. Basically static constructor is used to initialize the static variable of the class.
Syntax of static constructor
Static class_name(){            }
Features of the static Constructor
a)      There is no access modifier of static constructor
b)      There is only one static constructor in the class which means we can't overload the static constructor.
c)      Static constructor can only initialize the static variable of the class.
d)      We can’t pass any parameter to the static constructor.

Non static Constructor(also known as instance constructor)

These constructors are used to initialize the value of member variable of the class at the time of creation of object.
Type of non static constructor
a)      Default constructor
b)      Parameterized constructor
Default constructor
Default constructor is a constructor with no argument
Like
 public class_name()
{         }
Parameterized constructor
Parameterized constructor is a constructor with argument
Like public class_name(int x)
{                 }


Quess:-3 what is the difference between static and non static constructor
Ans:-
a)      Static constructor is called before the object creation but non static constructor is called at the time of object creation.
b)      There can only one static constructor in the class means we cannot overload the static constructors but we can overload non static constructor.
c)      There is no access modifier of static constructor but there is an access modifier of non static constructor.
d)      Static constructor can only initialize the static variable of class but non static constructor can initialize both static and non static variable of class.

Ques:- 4 Define Private Constructor?
Ans:-private constructor is those constructor which created with the private modifier. If any class contain only private constructor then
a)      We cannot create the object of this class
b)      We cannot inherit this class.
The class which contain only private constructor can only be used when we have only static member in the class in that case we need not to create the object of the class.

1 comment:

  1. Respected ma'am
    please explain the significance of the private constructor.i will be thankful to you

    ReplyDelete