Core Java - OOPs Concepts: Constructor Interview
Questions
27) What is the constructor?
The constructor can be defined as a special type of method that is used to
initialize the state of an object. It is invoked when the class is instantiated, and
the memory is allocated for the object. Every time an object is created using
the new keyword, the default constructor of the class is called. The name of
the constructor must be similar to the class name. The constructor must not
have an explicit return type.
To read more: Constructors in Java
28) How many types of constructors are used in Java?
Based on the parameters passed in the constructors, there are two types of
constructors in Java.
Default Constructor: The default constructor is the one that does not accept
any value. The default constructor is mainly used to initialize the instance
variable with the default values. The default constructor initializes instance
variables to their default values (numeric types to 0, booleans to false, and
object references to null). A default constructor is invoked implicitly by the
compiler if there is no constructor defined in the class.
public class Main {
// Default constructor (automatically provided if not defined explicitly)
}
Parameterized Constructor: The parameterized constructor is the one that can
initialize the instance variables with the given values. It allows us to initialize
the object with specific values during the object creation process. In other
words, we can say that the constructors that can accept the arguments are
called parameterized constructors.
public class Main {
// Parameterized constructor
public Example(int value) {
// Initialization logic here
}
}
29) What is the purpose of a default constructor?
The default constructor in Java is a constructor with no parameters. The
purpose of the default constructor is to assign the default value to the objects.
The Java compiler automatically provides it if a class does not have any
explicit constructor defined.
Example
class Main {
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Main m1=new Main ();
Main m2=new Main();
m1.display();
m2.display();
}
}
Compile and Run
Output:
0 null
0 null
Explanation: In the above class, you are not creating any constructor, so the
compiler provides you a default constructor. Here, 0 and null values are
provided by the default constructor.
30) Does the constructor return any value?
Yes, in Java, a constructor does not have a return type, not even void. The
purpose of a constructor is to initialize the object's state and prepare it for
use. The constructor implicitly returns the current instance of the class (we
cannot use an explicit return type with the constructor).
31) Is the constructor inherited?
No, Constructors are not inherited. The superclass constructor can be called
from the first line of a subclass constructor by using the keyword super and
passing appropriate parameters to set the private instance variables of the
superclass.
32) Can you make a constructor final?
No, in Java, constructors cannot be declared as final. The final keyword is
used to indicate that a class, method, or variable cannot be further
subclassed, overridden, or modified. However, it does not apply to
constructors.
33) Can we overload the constructors?
Yes, the constructors can be overloaded by changing the number of
arguments accepted by the constructor or by changing the data type of the
parameters.
Each constructor provides a different way to initialize an object of the class.
The choice of which constructor to invoke is based on the arguments passed
during object creation.
Consider the following example.
Example
class Test
{
int i;
public Test(int k)
{
i=k;
}
public Test(int k, int m)
{
System.out.println("Hi I am assigning the value max(k, m) to i");
if(k>m)
{
i=k;
}
else
{
i=m;
}
}
}
public class Main
{
public static void main (String args[])
{
Test test1 = new Test(10);
Test test2 = new Test(12, 15);
System.out.println(test1.i);
System.out.println(test2.i);
}
}
Compile and Run
Output:
Hi I am assigning the value max(k, m) to i
10
15
In the above program, the constructor Test is overloaded with another
constructor. In the first call to the constructor, the constructor with one
argument is called, and i will be initialized with the value 10. However, in the
second call to the constructor, the constructor with the 2 arguments is called,
and i will be initialized with the value 15.
34) What do you understand by copy constructor in Java?
There is no copy constructor in Java. However, we can copy the values from
one object to another, like a copy constructor in C++.
There are many ways to copy the values of one object into another in Java.
They are:
○ By using a constructor
○ By assigning the values of one object to another
○ By using Object.clone() Method
In this example, we are going to copy the values of one object into another
using a Java constructor.
Example
public class Main {
int id;
String name;
// Constructor to initialize integer and string
Main(int id, String name) {
this.id = id;
this.name = name;
}
// Copy constructor
Main(Main s) {
this.id = s.id;
this.name = s.name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Main obj1 = new Main(111, "Karan");
Main obj2 = new Main(obj1);
obj1.display();
obj2.display();
}
}
Compile and Run
Output:
111 Karan
111 Karan
Copy constructors can be useful in scenarios where you need to duplicate the
state of an existing object without directly referencing the same data. They are
not built into the Java language like some other languages, but we can define
them in our classes as needed.
To read more: Copy Constructor in Java
35) What are the differences between the constructors and
methods?
There are many differences between constructors and methods.
Java Constructor Java Method
A constructor is used to initialize the A method is used to expose the
state of an object. behavior of an object.
A constructor must not have a return A method must have a return type.
type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default The compiler does not provide the
constructor if we do not have any method in any case.
constructor in a class.
The constructor's name must be the The method name may or may not be
same as the class name. the same as the class name.
To read more: Difference Between Constructor and Method in Java
36) What is the output of the following Java program?
Example
public class Main
{
Main(int a, int b)
{
System.out.println("a = "+a+" b = "+b);
}
Main(int a, float b)
{
System.out.println("a = "+a+" b = "+b);
}
public static void main (String args[])
{
byte a = 10;
byte b = 15;
Main obj = new Main(a,b);
}
}
Compile and Run
Output:
a = 10 b = 15
Here, the data type of the variables a and b, i.e., byte, gets promoted to int, and
the first parameterized constructor with the two integer parameters is called.
37) What is the output of the following Java program?
Example
class Test
{
int i;
}
public class Main
{
public static void main (String args[])
{
Test test = new Test();
System.out.println(test.i);
}
}
Compile and Run
Output:
The output of the program is 0 because the variable i is initialized to 0
internally. As we know that a default constructor is invoked implicitly if there is
no constructor in the class, the variable i is initialized to 0 since there is no
constructor in the class.
38) What is the output of the following Java program?
class Test
{
int test_a, test_b;
Test(int a, int b)
{
test_a = a;
test_b = b;
}
public static void main (String args[])
{
Test test = new Test();
System.out.println(test.test_a+" "+test.test_b);
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
The constructor Test() is undefined
There is a compiler error in the program because there is a call to the default
constructor in the main method that is not present in the class. However, there
is only one parameterized constructor in the class Test. Therefore, no default
constructor is invoked by the constructor implicitly.