• Definition: The constructor is a specialized method for initializing objects.Name of the constructor is same as that of its class name. In other words, the name of the constructor and class name is same.
Constructor
• Definition:
The constructor is a specialized method for initializing objects.
• Name
of the constructor is same as that of its class name. In other words, the name
of the constructor and class name is same.
• Whenever
an object of its associated class is created, the constructor is invoked
automatically.
• The
constructor is called constructor because it creates values for data fields of
class.
• Example
-
Java
Program[Rectangle1.java]
public
class Rectangle 1 {
int
height;
int
width;
Rectangle1()
{
System.out.println("
Simple constructor: values are initialised...");
height=10;
width=20;
}
Rectangle
1 (int h,int w)
{
System.out.println("
Parameterised constructor: values are initialised...");
height=h;
width=w;
}
void
area()
{
System.out.println("Now,
The function is called...");
int
result height*width;
System.out.println("The
area is "+result);
}
class
Constr_Demo {
public
static void main(String args[])
{
Rectangle1
obj = new Rectangle 1();
obj.area();//call
the to method
Rectangle1
obj1 = new Rectangle 1(11,20);
obj1.area();//call
the to method
}
}
Output
F:\test>javac
Rectangle1.java
F:\test>java
Constr_Demo
Simple
constructor: values are initialised...
Now, The
function is called...
The area
is 200
Parameterised
constructor: values are initialised...
Now, The
function is called...
The area
is 220
F:\test>
Program Explanation
In above
program, there are two classes - Rectangle1 and Constr_Demo. In the Rectangle1
class, data fields, method and constructor is defined. In the Constr_Demo class
the objects are created. When an object obj gets created, then the simple
constructor Rectangle1() gets invoked and the data fields height and width gets
initialized
Hence
the area function will compute the area=10*20=200.
When an
object obj1 gets created, then the parameterised constructor Rectangle1(11, 20)
gets invoked and the data fields height and width gets initialised.
Hence
the area function will compute the area = 11*20 = 220.
The
class Constr_Demo is called main class because it consists of main function.
1. Name
of constructor must be the same as the name of the class for which it is being
used.
2. The
constructor must be declared in the public mode.
3. The
constructor gets invoked automatically when an object gets created.
4. The
constructor should not have any return type. Even a void data type should not
be written for the constructor.
5. The
constructor cannot be used as a member of union or structure.
6. The
constructors can have default arguments.
7. The
constructor cannot be inherited. But the derived class can invoke the
constructor of base class.
8.
Constructor can make use of new or delete operators for allocating or releasing
memory respectively.
9.
Constructor can not be virtual.
10.
Multiple constructors can be used by the same class.
11. When
we declare the constructor explicitly then we must declare the object of that
class.
Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : Definition, Operation, Properties with Example Java Programs - Constructor
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation