Answer:
The answer to this question can be given as:
In this class definition, there are two constructors.
Explanation:
In the class definition two constructors are different in type but first we explain constructor that can be as:
Constructor: constructor are special member functions whose task is to initialized as an object of its class.
Rule for defining constructor:
A constructor doesn’t have a return type not even void.
The name of the constructor must be the same as the class name.
A constructor is called automatically when a new instance of an object is created.
Type of constructor:
- default constructor.
- parameterized constructor.
- copy constructor.
In the question there are two type of constructor is used that can be given as:
default constructor:
The constructor without any parameters is called a default constructor. This type of constructor is called automatically when a new instance of an object is created.
parametrized constructor:
In the parameterized constructor we use at least one parameter in the constructor that is called the parameterized constructor. In the parameterized constructor we can initialize each instance of the class with several values.
Example :
class AB //define class
{
String name; //define variable
AB() //default constructor
{
System.out.print("hello..."); //message.
}
AB(String name) //parametrized constructor.
{
this.name = name; //holding value in name variable.
}
}
class Main //define class
{
public static void main (String[] args) //main method
{
AB OB1 =new AB(); //creating class object.
AB ob2=new AB("XYZ");
System.out.print(ob2.name); //print value
}
}
Output:
hello... XYZ