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 method overloading and method overriding?

    Overloading means several methods in the same class share a name but take different parameters, and the compiler picks one. Overriding means a subclass gives its own version of a method it inherited, with the same signature, and the JVM picks the version at runtime based on the actual object.

    BeginnerIn-depth3 min read
  2. What is the difference between compile-time and runtime polymorphism?

    Compile-time (static) polymorphism is resolved by the compiler using the declared types, mainly through method overloading. Runtime (dynamic) polymorphism is resolved by the JVM using the actual object, through method overriding and dynamic dispatch. Fields, static methods and private methods are never resolved at runtime.

    AdvancedIn-depth4 min read
  3. What is duck typing in Python, and how does it relate to polymorphism?

    Duck typing means an object is accepted if it has the methods you call, regardless of its class. It gives polymorphism without a shared parent or interface.

    BeginnerQuick answer1 min read
  4. How can polymorphism replace a long if-else or switch chain?

    Give each case its own class with the same method, then call that method on the object. Adding a new case means adding a class, not editing the old, tested chain.

    IntermediateQuick answer1 min read
  5. 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
  6. Predict the output: a parent reference holds a child object that hides the parent's field. Which value do you get?

    Methods are chosen by the object's real type, but fields are chosen by the reference type. So p.name gives the parent's value while p.get() runs the child's method.

    AdvancedQuick answer1 min read