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 the difference between an abstract class and an interface?

    An abstract class is a partially built parent that can hold state, constructors and shared code, and a class can extend only one. An interface is a contract of methods that a class promises to provide, and a class can implement many.

    IntermediateIn-depth2 min read
  2. 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
  3. 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
  4. Can static methods be overridden in Java?

    No. A static method in a child with the same signature hides the parent's one. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.

    IntermediateQuick answer1 min read
  5. How do you create an immutable class in Java?

    Make the class final, all fields private and final, set them only in the constructor, add no setters, and copy any mutable object on the way in and on the way out.

    IntermediateQuick answer1 min read
  6. How do you make a Singleton thread-safe in Java?

    Use eager initialisation, the holder class idiom, or an enum. If you need lazy creation with double-checked locking, the field must be volatile.

    IntermediateQuick answer1 min read
  7. What happens in memory when you write new Student("Asha") in Java?

    new allocates memory for the object on the heap, sets fields to default values, runs the constructor, and returns a reference. The variable on the stack holds only that reference.

    IntermediateQuick answer1 min read
  8. What is dynamic method dispatch in Java?

    It is the way Java chooses which overridden method to run at runtime, based on the actual object type and not on the type of the reference variable.

    IntermediateQuick answer1 min read
  9. What is the difference between protected and default (package-private) access in Java?

    Default access lets only classes in the same package use the member. Protected allows the same package plus subclasses in any package, but through inheritance.

    IntermediateQuick answer1 min read
  10. When does an object become eligible for garbage collection in Java?

    An object becomes eligible when no live thread can reach it through any chain of references. The JVM then frees its memory at a time of its own choosing.

    IntermediateQuick answer1 min read
  11. Why does Java not allow a class to extend two classes?

    To avoid the diamond problem: if two parents define the same method and a child inherits both, it is unclear which version should run. Java blocks it and lets you combine interfaces instead.

    IntermediateQuick answer1 min read