Friday, April 21, 2017

All about Java Constructors Interview Questions and Answers

What is a Constructor?

Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.

What is Copy Constructor in Java?

Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.

To copy the values of one object into another in java, you can use:
     
  • Assigning the values of one object into another
  • clone() method of Object class
  • Constructors
What is Constructor Chaining ?

Constructor Chaining is a basically, calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.

Sample Code:

/**
 * @author Abhinaw.Tripathi
 *
 */
public class ConstructorChaining {


public ConstructorChaining()
{
this(5);
               System.out.println("The Default constructor");
        
}
public ConstructorChaining(int x) 
{
this(5, 15);
               System.out.println(x);
}

public ConstructorChaining(int i, int j)
{
System.out.println(i * j);
}

public static void main(String[] args) 
{
// TODO Auto-generated method stub
       new ConstructorChaining();
}

}

Output:

75
5
The Default constructor

Can we call sub class constructor from super class constructor?

No.There is no way you can do this in java.


What happens if you keep a return type for a constructor?

Ideally, Constructor can not have a return type By definition, if a method has a return type, it’s not a constructor.(JLS8.8 Declaration) It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name.

Example:

public class ConstructorChaining {


int ConstructorChaining()
{
            return 0;
        }
public static void main(String[] args) 
{
            new ConstructorChaining();
}

}

What is the difference between argument constructor and default Constructor?

Ans:

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.


What are private constructors and where are they used?

Ans:

Like any method we can provide access specifier to the constructor. If it’s made private, then it can only be accessed inside the class.

Lets take example, we use private constructor:

Internal Constructor chaining
Singleton class design pattern

When do we need Constructor Overloading?

Ans:

Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. Different constructors can do different work by implementing different line of codes and are called based on the type and no of parameters passed.
According to the situation , a constructor is called with specific number of parameters among overloaded constructors.

Do we have destructors in Java?

Ans:

No, Because Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor.





No comments: