Constructor Explanation - Notes by Shariq SP

Understanding Constructors in Java

In Java, a constructor is a special type of method that is invoked automatically when an object of a class is created. It is used to initialize the state of an object by assigning initial values to its instance variables or performing any necessary setup tasks.

Why Constructors are Needed?

Constructors are needed for the following reasons:

  • To initialize the state of an object.
  • To ensure that all required fields of an object are properly initialized.
  • To perform any necessary setup tasks before the object can be used.

Syntax of a Constructor

The syntax of a constructor is as follows:


            public class ClassName {
                // Constructor declaration
                ClassName() {
                    // Constructor body
                    // Initialization code goes here
                }
            }
            

Example of a Constructor

Here's an example of a constructor in Java:


            public class Person {
                String name;
                int age;
                
                // Constructor
                public Person(String name, int age) {
                    this.name = name;
                    this.age = age;
                }
            }
            

Types of Constructors

There are several types of constructors in Java:

  1. Default Constructor: A constructor with no parameters. If no constructor is explicitly defined, Java provides a default constructor automatically.
  2. Parameterized Constructor: A constructor with parameters that allows initialization of object properties with provided values.
  3. Copy Constructor: A constructor that initializes an object using another object of the same class.

Example of Default Constructor

The default constructor is automatically provided by Java if no constructor is explicitly defined. It has no parameters and initializes instance variables with default values.


            public class Car {
                String make;
                int year;
                
                // Default Constructor
                public Car() {
                    make = "Unknown";
                    year = 0;
                }
            }
            

Example of Parameterized Constructor

A parameterized constructor allows initialization of object properties with provided values:


            public class Student {
                String name;
                int age;
                
                // Parameterized Constructor
                public Student(String name, int age) {
                    this.name = name;
                    this.age = age;
                }
            }
            

Example of Copy Constructor

A copy constructor initializes an object using another object of the same class:


            public class Point {
                int x, y;
                
                // Copy Constructor
                public Point(Point p) {
                    this.x = p.x;
                    this.y = p.y;
                }
            }
            

The default constructor is called automatically when an object is created without specifying any arguments.

Interview Questions on Constructors

  1. What is a constructor in Java?
  2. Why are constructors used?
  3. What is the difference between a constructor and a method?
  4. Can a constructor have a return type?
  5. What is constructor overloading?
  6. Can you have multiple constructors in a class?
  7. What is the purpose of a default constructor?
  8. How do you call a superclass constructor from a subclass constructor?
  9. What is the significance of the this keyword in constructors?
  10. Can constructors be inherited?

Multiple Choice Questions (MCQs) - Constructors

  1. Which of the following statements about constructors in Java is true?
    1. A constructor can be declared as private.
    2. A constructor cannot have parameters.
    3. A constructor can return a value.
    4. A constructor can be inherited.
  2. What is the purpose of constructor overloading?
    1. To create multiple instances of the same class.
    2. To provide different ways to initialize objects of the class.
    3. To restrict access to the class members.
    4. To extend the functionality of the class.
  3. Which constructor is invoked when an object is created without providing any arguments?
    1. Parameterized constructor
    2. Copy constructor
    3. Default constructor
    4. Static constructor
  4. Can a constructor be abstract?
    1. Yes
    2. No
    3. Depends on the access specifier
    4. Only in interfaces
  5. Which keyword is used to call a superclass constructor from a subclass constructor?
    1. super
    2. this
    3. extends
    4. inherit