Constructor Definition:
•A constructor is a special type of method which has the same name as the class name in which it is declared and is used to initialize the instance variables of a class.
Constructor Characteristics:
•Constructor has the same name as the class name in which it is declared.
•Constructor has no return type.
•The implicit return type of a constructor is the class(type) itself.
•Constructor is invoked automatically when an object is created.
•Besides initialization statements, we can write any kind off statements in the constructor.
•When no constructor is declared, the default constructor is implicitly invoked to initialize the object.
Declaring a Constructor:
•Syntax for declaring a constructor is as follows:
ClassName()
{
statement(s);
}
Example of a Constructor:
•Let’s see a sample constructor:
class Box
{
int length, width, height;
Box() <------ Constructor
{
length = 10; width = 20; height = 30;
}
}
Types of Constructors:
•There are 3 types of constructors. They are:
1) Parameter less / Zero parameter constructor
2) Parameterized constructor
3) Copy constructor
Parameter Less Constructor:
•This constructor has no parameters.
Example:
Box()
{
length = 10;
width = 20;
height = 30;
}
Parameterized Constructor:
•This constructor accepts one or more parameters.
Example:
Box(int l, int w, int h)
{
length = l;
width = w;
height = h;
}
Copy Constructor:
•This constructor is used to create an exact copy of the already existing object. This constructor accepts a parameter of the “class” type.
Example:
Box(Box ob)
{
this.length = ob.length;
this.width = ob.width;
this.height = ob.height;
}
Invoking Constructors:
Constructors are automatically invoked when the objects are created.
•Invoking a parameter less constructor:
Example: Box b = new Box();
•Invoking a parameterized constructor:
Example: Box b = new Box(10,20,30);
•Invoking a copy constructor:
Example:
Box b1 = new Box(10,20,30);
Box b2 = new Box(b1);
•A constructor is a special type of method which has the same name as the class name in which it is declared and is used to initialize the instance variables of a class.
Constructor Characteristics:
•Constructor has the same name as the class name in which it is declared.
•Constructor has no return type.
•The implicit return type of a constructor is the class(type) itself.
•Constructor is invoked automatically when an object is created.
•Besides initialization statements, we can write any kind off statements in the constructor.
•When no constructor is declared, the default constructor is implicitly invoked to initialize the object.
Declaring a Constructor:
•Syntax for declaring a constructor is as follows:
ClassName()
{
statement(s);
}
Example of a Constructor:
•Let’s see a sample constructor:
class Box
{
int length, width, height;
Box() <------ Constructor
{
length = 10; width = 20; height = 30;
}
}
Types of Constructors:
•There are 3 types of constructors. They are:
1) Parameter less / Zero parameter constructor
2) Parameterized constructor
3) Copy constructor
Parameter Less Constructor:
•This constructor has no parameters.
Example:
Box()
{
length = 10;
width = 20;
height = 30;
}
Parameterized Constructor:
•This constructor accepts one or more parameters.
Example:
Box(int l, int w, int h)
{
length = l;
width = w;
height = h;
}
Copy Constructor:
•This constructor is used to create an exact copy of the already existing object. This constructor accepts a parameter of the “class” type.
Example:
Box(Box ob)
{
this.length = ob.length;
this.width = ob.width;
this.height = ob.height;
}
Invoking Constructors:
Constructors are automatically invoked when the objects are created.
•Invoking a parameter less constructor:
Example: Box b = new Box();
•Invoking a parameterized constructor:
Example: Box b = new Box(10,20,30);
•Invoking a copy constructor:
Example:
Box b1 = new Box(10,20,30);
Box b2 = new Box(b1);
No comments:
Post a Comment