This article we will explain about constructors and
how many types of constructors. What is the main use of constructors with
example.
Constructors:
Before discuss about it first we know what is
constructor.
Constructor is a special type of method that enables
an object to initialize itself when it is created. It has the same name as the
class itself. They do not specify a return type, not even void. This is because
they do not return any value. Constructor is usually public because they are
provided to create objects. It can also
be declared as private or protected. In
such case objects of that class cannot be created and also the class cannot be
used as a base class for inheritance.
Example of constructor:
Class
Rectangle
{
Public int length;
Public int width;
Public Rectangle(int x , int y) // constructor
method
{
Length = x;
Width = y;
}
Public int RectArea()
{
Return(length * width);
}
}
Overload
Constructors:-
Overload constructor follow the same concept of
method overloading in c#. Method overloading means same name but different
parameter lists and different definitions.
To create an overloading constructor method we have to do several
different constructor definitions with different parameter lists. The
difference may be in either the number or type of arguments.
Example of overloaded
construstors:
Class Room
{
Public double length;
Public double breadth;
Public
Room(double x, double y)// first one
{
Length =x;
Breadth =y;
}
Public Room(double x)// second one
{
Length = breadth = x;
}
}
Types of
constructors:-
Basically 5 types of constructors available.
These are:-
1:-Default
Constructor.
2:-Parameterized
constructor.
3:-Copy constructor(c sharp doesn’t support it)
4:-Static
constructor.
5:-Private
constructor.
Default Constructor:-
It is a
constructor type created without parameter. If we do not create any constructor
in a class it created automatically.
Parameterized constructor:-
A
constructor having one or more parameters is called parameterized constructor.
Copy constructor:-
A copy
constructor creates an object by copying variables from another object. This
type of constructor c# doesn’t support. We will not explain much more about it
here.
Static constructor:-
Static
constructor is declared by prefixing a static keyword to the constructor
definition. It cannot have any parameter.
No access modifier on static constructors. A class can have only one
static constructor.
Private constructor:-
It is a
constructor that declared with private access modifier in a class.
We cannot
create an object which class contain private constructor.
Which class
contain private constructor we cannot inherited that class. To access the
methods or properties in a class with private constructor we can assign methods
or properties with static keyword.
Some important points about private constructor:-
1- We can
use it when we have only static member.
2-It use for
implementation of singleton class pattern.
3-If we want
to create object of the class which contain private constructor we can have a
set of public constructor along with the private constructor.
No comments:
Post a Comment