Interview Q&A / Object-Oriented Programming

INTERVIEW Q&A · 69 QUESTIONS

Object-Oriented Programming interview questions

The four pillars, interfaces, abstract classes and design basics.

  1. What is a constructor, and what is constructor chaining?

    A constructor is a special method with the class's name and no return type that runs when an object is created, to set its starting state. Constructor chaining is one constructor calling another, either in the same class with this(...) or in the parent class with super(...), so setup code is written once.

    BeginnerIn-depth3 min read
  2. When does Java provide a default constructor, and when does it not?

    If you write no constructor at all, the compiler adds a public no-argument one. The moment you write any constructor yourself, the compiler stops adding it.

    BeginnerQuick answer1 min read
  3. Are constructors and private members inherited in Java?

    Constructors are never inherited; a child must define its own and call the parent's with super(). Private members exist inside the child object but cannot be accessed by name from the child.

    IntermediateQuick answer1 min read
  4. Can an abstract class have a constructor? Why would it need one?

    Yes. It cannot be instantiated directly, but a subclass constructor calls it with super() so the parent's fields get initialised.

    IntermediateQuick answer1 min read