Java Interview Questions - Constructor and Object

1. What is constructor and its types?

Constructor a special type of method that is used to initialize the state of an object. Constructor is invoked at the time of object creation. There are two types of constructors:

  • Default constructor (no-argument constructor)
  • Parameterized constructor.

2. What are the difference between method and constructor?

Constructors must have the same name as the class and cannot return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.

3. How would you make a copy of an entire Java object with its state?

By object copy i.e. copy constructor.

4. What is copy constructor?

A copy constructor is a constructor that takes only one parameter which is the same exact type as the class in which the copy constructor is defined.

5. What is factory method?

When return type of method is same as class name is known as factory method.

6. What is singleton class?

If object will create only for a single time maximum it is known as single tone class.

7. For single tone class which method is compulsory?

Factory method is compulsory for single tone class.

8. What is reference?

Reference is just like a pointer in java which refers to object.

9. What is the use of reference?

Reference is used to access the members of a class repeatedly without creating a new memory. In other words user can reuse the object by the help of reference.

10. What is private constructor?

If a constructor declared with private modifier then it is known as private constructor and it is used to protect the class to instantiate from outside of that class.

11. Use of default and Parameterized constructor in java?

Default constructor is used to initialize default value to all objects whereas parameterized constructor is used to initialized different value to different objects.

12. Which class should you use to obtain design information about an object?

The Class is used to obtain information about an object’s design.

13. Can you call one constructor from another if a class has multiple constructors?

Yes. Use this () to call a constructor from an other constructor.

14. Can constructor be inherited?

No

15. What is constructor chaining and how is it achieved in Java?

A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.

16. Can an unreachable object become reachable again?

Yes, an unreachable object may become reachable again. This happens when the objects finalize () method is invoked.

17. What is an Object and how do you allocate memory to it?

An object is nothing but instance of the class. By using “new” operator we can allocate the memory of an object in java.

18. Can we declare a class as Abstract without having any abstract method?

Yes abstract may or may not contains any abstract method.

19. What is the difference between this() and super()?

This is used to refers the current object of same class whereas super is used to refers the current object of super class.

20. What is object cloning? Why it is needed?

The object cloning is a way to create exact copy of an object. For this purpose, clone () method of Object class is used to clone an object.

21. What’s the difference between constructors and methods?

Constructors must have the same name as the class and cannot return a value. They are only called once while regular methods could be called many times.

22. Does all property of immutable object needs to be final?

No not required.