Constructor Interview Questions
Constructor Interview Questions
Java constructor is a unique method that initializes the objects, which is called
when an instance of the class is created.
The memory for the object is allocated when we call the constructor.
* Default Constructor
* Non-parameterized Constructor
* Parameterized Constructor
<class name>() {}
Example:
Employee()
{
//some code
}
Employee(int i, String n)
{
id = i;
name = n;
}
________________________________________________________________________
3) Do we have a copy constructor in Java?
The following are some methods to copy the values from one object to another:
By constructor
By assigning the values of one object to another
By clone() method of Object class
Code:
[Complete the code in has a relation concept by using copy constructor ]
@Override
public String toString() {
return "Student [name=" + name + ", addres=" + addres + ", adharNo=" +
adharNo + ", age=" + age + "]";
}
public University() {
super();
}
University(String name,String university_name,Student s){
Student s2=new Student(s.name, s.addres,s.adharNo, s.age);
this.name=name;
this.university_address=university_name;
// s1.name=s.name;
// s1.addres=s.addres;
// s1.adharNo=s.adharNo;
//s1.age=s.age;
}
@Override
public String toString() {
return "University [name=" + name + ", university_address=" +
university_address + ", s1=" + s1 + "]";
}
________________________________________________________________________
4) Write a Java Program to Copy the values from one object to another Object ?
ConstructorDemo.java:
class ConstructorDemo{
int id;
String name;
ConstructorDemo(ConstructorDemo c){
id = c.id;
name =c.name;
}
void display(){
System.out.println(id+" : "+name);
System.out.println(name+" : "+name);
}
The subclass constructor has its own private data members, so Java does not provide
any way to access the sub-class constructor from a super class constructor.
However, we can call a superclass constructor from a sub-class constructor by using
the super keyword.
_______________________________________________________________________________
6) Can we have a constructor in the Interface?
Constructor Chaining is a way to call one constructor from another constructor with
respect to the current object. It can be achieved in the following two ways:
From base class: We can use the super keyword to call a constructor from the base
class.
Within the same class: We can call a constructor within the same class by using
this() keyword.
class TestSuper
{
public TestSuper(int i)
{
System.out.println("Super Class Constructor");
}
}
public TestSub(int i)
{
super(i); //Calling super class constructor
}
}
Example 2:
---------------------
class A {
int a;
A() {
this(12);
}
A(int a) {
this(8, 9);
}
A(int a, int b) {
this.a = a;
}
}
class B extends A {
B() {
this(4);
}
B(int a) {
this(5, 6);
}
B(int a, int b) {
super();
System.out.println("hello "+super.a);
}
}
___________________________________________________________________________________
_________________________________________________________________________________
9) What is a private constructor?
Like methods, we can have the private constructors in Java. To make or create a
constructor as private, use the private keyword while declaring it. It can only be
accessed within that class.
The following are some usage scenarios when we need a private constructor:
class Singleton{
private static Singleton instance=null;///1000x
private Singleton() {
System.out.println("CONSTRUCTOR CALLED THROUGH OBJECT
CREATION!!!");
}
}
}
__________________________________________________________________________
10) Why constructors in Java cannot be static?
Generally, when we declare a method as protected, other classes can access that
method in a different package by using inheritance only. But, when we declare a
constructor protected, it behaves slightly differently than a method. The protected
constructor can only be accessed by using a super keyword according to Java
language standards.
__________________________________________________________________________
14) Why constructor name is similar to the class name?
When we create an object of a class using a new keyword, it should have information
about that particular class. That is why the constructor's name must be similar to
the class name.
__________________________________________________________________________
15) Why return type is not allowed for the constructor?
The return type is not allowed in the constructor because if we provide a return
type in the constructor, it will act as the normal method. So, to differentiate
between constructor and method block, the return type is not allowed in
constructors.
__________________________________________________________________________
16) Can we write this() and super() in same constructor ?
___________________________________________________________________________
17) Can we override the Constructor ?
___________________________________________________________________________
18) Can we overload the Constructor ?
___________________________________________________________________________
19) How many modifiers are allowed to the constructor ?
___________________________________________________________________________
20) Can we write a return type to the constrcuctor ?
___________________________________________________________________________
21) Can we write a empty class ?
____________________________________________________________________________
22) If we write any constructor by ourself in the class then
compiler will add the constructor by itself ?
_____________________________________________________________________________
23) What is the modifiers of default constructor if program will
not write the constructor by it's own?
______________________________________________________________________________
24) Can we write super and this both in one constructor ?
______________________________________________________________________________
25) Is it possible for a class to have multiple constructors in java?
______________________________________________________________________________
26) Is it possible to inherit a constructor?
______________________________________________________________________________
27) What are possible access modifiers that can be marked for a constructor?
______________________________________________________________________________
28) When does Java compiler define the default constructor?
______________________________________________________________________________
29) Why a constructor defined by Java compiler is always called as default
constructor?
_______________________________________________________________________________
30) What will be the ouput of the following program
________________________________________________________________________________
31)
Will the below code compile successfully?
If yes, what will be the output of the following program?
public class Test
{
Test() {
this(20);
System.out.println("One");
}
Test(int i) {
this(null);
System.out.println("Two");
}
Test(Test test) {
System.out.println("Three");
}
public static void main(String[] args)
{
new Test();
}
}
__________________________________________________________________________________
32)Can we have more than one constructor with same signature in a class?
__________________________________________________________________________________
33)Can we create an object of class within the same class if a constructor is
marked with private?
_________________________________________________________________________________
34) Can class be extended when a constructor is declared private?
__________________________________________________________________________________
35) What is the difference between constructor and method?