- What is a constructor in object-oriented programming?
- A constructor is a special method that is automatically called when an instance of a class is created. It is used to initialize the object.
class Example {
Example() {
System.out.println("Constructor called");
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(); // Constructor is called here
}
}
- How does a constructor differ from a regular method?
- A constructor is called automatically when an object is created, while regular methods need to be called explicitly. Constructors also do not have a return type, not even void.
class Example {
Example() {
System.out.println("Constructor called");
}
void regularMethod() {
System.out.println("Regular method called");
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(); // Constructor is called here
obj.regularMethod(); // Regular method is called explicitly
}
}
- What is the purpose of a constructor in a class?
- The purpose of a constructor is to initialize the object’s state by assigning values to the object’s properties.
class Example {
int value;
Example(int val) { // Constructor with parameter
value = val;
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(10); // Constructor initializes value to 10
}
}
- When is a constructor called in the lifecycle of an object?
- A constructor is called immediately when an object is created.
class Example {
Example() {
System.out.println("Constructor called");
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(); // Constructor is called here
}
}
- Can a class have more than one constructor? If so, how?
- Yes, a class can have more than one constructor through constructor overloading, which means having multiple constructors with different parameters.
class Example {
Example() {
System.out.println("Default constructor called");
}
Example(int val) {
System.out.println("Parameterized constructor called with value: " + val);
}
}
public class Main {
public static void main(String[] args) {
Example obj1 = new Example(); // Calls default constructor
Example obj2 = new Example(10); // Calls parameterized constructor
}
}
- What is the difference between a default constructor and a parameterized constructor?
- A default constructor does not take any parameters, while a parameterized constructor takes one or more parameters to initialize the object with specific values.
class Example {
int value;
Example() { // Default constructor
value = 0;
System.out.println("Default constructor called");
}
Example(int val) { // Parameterized constructor
value = val;
System.out.println("Parameterized constructor called with value: " + val);
}
}
public class Main {
public static void main(String[] args) {
Example obj1 = new Example(); // Calls default constructor
Example obj2 = new Example(10); // Calls parameterized constructor
}
}
- What happens if you do not provide a constructor in a class?
- If you do not provide a constructor, the compiler automatically provides a default constructor that initializes the object with default values.
class Example {
// No constructor provided
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(); // Default constructor is called
}
}
- Explain constructor overloading with an example.
- Constructor overloading is having multiple constructors in a class with different parameter lists to allow different ways of initializing the object.
class Example {
Example() {
System.out.println("Default constructor called");
}
Example(int val) {
System.out.println("Parameterized constructor called with value: " + val);
}
}
public class Main {
public static void main(String[] args) {
Example obj1 = new Example(); // Calls default constructor
Example obj2 = new Example(10); // Calls parameterized constructor
}
}
- Can a constructor be private? If yes, in what scenarios is this useful?
- Yes, a constructor can be private. This is useful in scenarios like implementing the Singleton pattern, where you want to restrict the creation of the object to one instance only.
class Singleton {
private static Singleton instance;
private Singleton() {
// Private constructor
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton obj1 = Singleton.getInstance();
Singleton obj2 = Singleton.getInstance();
System.out.println(obj1 == obj2); // true, both references point to the same instance
}
}
- What is the purpose of a copy constructor?
- A copy constructor is used to create a new object as a copy of an existing object. It initializes a new instance of a class using the data of an existing instance.
class Example {
int value;
Example(int val) {
value = val;
}
// Copy constructor
Example(Example obj) {
value = obj.value;
}
}
public class Main {
public static void main(String[] args) {
Example obj1 = new Example(10); // Calls parameterized constructor
Example obj2 = new Example(obj1); // Calls copy constructor
System.out.println("Value of obj2: " + obj2.value); // Should print 10
}
}
- What is constructor chaining and how is it implemented in Java?
- Constructor chaining is the process of calling one constructor from another constructor in the same class (using
this()) or in the superclass (usingsuper()). This helps to avoid code duplication and provides a clear structure for constructors.
- Constructor chaining is the process of calling one constructor from another constructor in the same class (using
class Example {
int value1;
int value2;
Example() {
this(10, 20); // Calls the parameterized constructor
}
Example(int val1, int val2) {
value1 = val1;
value2 = val2;
}
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(); // Default constructor chains to parameterized constructor
System.out.println("Value1: " + obj.value1 + ", Value2: " + obj.value2); // Should print Value1: 10, Value2: 20
}
}








