OOP Interview Question

What is a constructor in OOP?

Updated 2026-08-16 · Beginner friendly
Quick answer

A constructor is a special method that runs automatically when an object is created. Its job is to set the starting values of the object so it begins life in a valid state. A constructor has the same name as the class and has no return type. If you do not write one, the compiler provides a default constructor.

Key takeaways
  • A constructor is a special method that runs automatically when an object is created.
  • It shares the class name, has no return type, and sets the object's starting values.
  • If you write none, the compiler supplies a default constructor with no arguments.

Why constructors exist

When you create an object you often want it to start with sensible values. A constructor lets you set those values in one place, so no object is ever created half finished. It runs once, at the moment the object is made.

class Student {
public:
    string name;
    int age;

    Student(string n, int a) {   // constructor
        name = n;
        age = a;
    }
};

Student s("Asha", 20);  // constructor runs here
class Student {
    String name;
    int age;

    Student(String n, int a) {   // constructor
        name = n;
        age = a;
    }
}

Student s = new Student("Asha", 20);  // constructor runs here
class Student:
    def __init__(self, name, age):   # constructor
        self.name = name
        self.age = age

s = Student("Asha", 20)  # __init__ runs here

Types of constructors

Key rules

A constructor has the same name as the class and no return type, not even void. You can have more than one constructor in a class by giving them different parameters, which is constructor overloading.

In the interview

A common follow up is the difference between a constructor and a normal method. The constructor has no return type and runs automatically on object creation, while a method has a return type and runs only when you call it. State that clearly and you look confident.

Frequently asked questions

What is the difference between a constructor and a method?

A constructor has no return type and runs automatically on object creation, while a method has a return type and runs only when you call it.

What is constructor overloading?

It is defining several constructors in one class with different parameters, so you can create an object in more than one way.

Want the full OOP guide?

Read every OOP concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the OOP guide All OOP questions