1) What is Constructor?
A) Constructors are the functionalities which are going to get executed automatically at the time of creation of an object.
2) What is the difference between Constructor & Methods?
|1) With respect to one object, a constructor could be executed only once.||1) With respect to one object, a method could be executed any number of times.|
|2) Constructors are executed automatically at the time of creation of an object.||2) A method would be executed only it is explicitly called.|
|3) A constructor cannot have any return type not even void.||3) A method should and must have a return type at least void is compulsory.|
|4) Name of the constructor cannot be anything. Name of the constructor should and must be the name of the class.||4) Name of the method can be anything & the name of the method can also be the name of the class.|
3) Can we define a method with the same name class name?
A) Yes, it is allowed to define a method with the same class name. No compile time error and no runtime error is raised, but it is not recommended as per coding standards.
4) How compiler and JVM can differentiate constructor and method definitions if both have the same class name?
A) By using return type, if there is a return type it is considered as method else it is considered as a constructor.
5) How Compiler and JVM can differentiate constructor and method invocations if both have the same class name?
A) By using the new keyword, if the new keyword is used in calling then the constructor is executed else method is executed.
6) Can we declare constructor as private?
A) Yes, we can declare constructor as private. All four accessibility modifiers are allowed for the constructor. We should declare constructor as private to not allow the user to create an object from outside of class. Basically, we will declare constructor as private to implement a singleton design pattern.
7) How many types of constructors are there in java?
A) Java supports “3” types of constructors:
- Default constructor.
- No-argument (or) Non-parameterized constructor.
- Parameterized constructor.
8) What is Default constructor?
A) The zero-argument constructor which is automatically provided to the class by the compiler (because the class doesn’t one constructor) at the time of generating the byte code is known as Default constructor.
9) Why compiler generated constructor is called default constructor?
A) Because it obtains all its default properties from its class they are:
· Its accessibility modifier is same as its class accessibility modifier.
· Its name is same as its class name.
· It does not have parameters and logic.
10) Can we consider both default and non-argument are same?
A) No, both are different. They seem to be the same but really not the same.
11) What are the differences between no-argument and default constructor?
|Default constructor||No-argument constructor|
|1) It is generated by the compiler.||1) It is defined by the developer.|
|2) Its accessibility modifier is same as class accessibility modifier. So the only allowed accessibility modifiers are default or public.||2) It can have all four accessibility modifiers as it is defined by the developer, so the allowed accessibility modifiers are private, default, public, protected.|
|3) It does not have logic except super() call.||3) It can have logic including super() call.|
12) Is constructor definition mandatory in class?
A) No, it is optional. If we do not define constructor compiler defines the constructor.
Example: Developer written code
public static void main(String args)
Compiler changed code
public static void main(String args)
13) What is Constructor Overloading?
A) Defining multiple constructors within the same class by changing the data types of the parameters & parameters is known as Constructor Overloading.
14) Why should we overload the constructor?
A) To define different object initialization logic. To execute the same initialization logic by taking input values in a different type.
15) What is constructor chaining?
A) Calling one constructor from another constructor by using super() and this() is called constructor chaining.
16) Explain about “this” operator?
A) The functionality of “this” operator is to explicitly point the current object.
17) What is the current object?
A) The object because of which the method is currently under execution is known as the current object.