ICSE Class 10 Computer Applications
4. Constructor
01 1. Defination of constructor and characteristics
​
In Java, every class has its constructor that is invoked automatically when an object of the class is created.
A constructor has the same name as that of class and it does not return any value.
class Test
{
Test()
// constructor body
}
}
02 Types of constructors
Non-Parameterized Constructor(default)
A constructor that accepts no parameter.
Parameterized Constructor
we can pass parameters to a constructor. Such constructors are known as a parameterized constructor.
03 Use of constructor
Constructor is used to initialise an object of a class.
​
04 Constructor overloading
​
•Just like method overloading, constructors also can be overloaded.
​
•Same constructor declared with different parameters in the same class is known as constructor overloading.
•Compiler differentiates which constructor is to be called depending upon the number of parameters and their sequence of data types.
​
05 Difference between constructor and method
​
-
Constructor is used to initialize an object whereas method is used to exhibits functionality of an object.
-
Constructors are invoked implicitly whereas methods are invoked explicitly.
-
Constructor does not return any value where the method may/may not return a value.
-
In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided.
-
Constructor should be of the same name as that of class. Method name should not be of the same name as that of class. ​