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 are the four pillars of object-oriented programming?

    Encapsulation (keep data and the code that uses it together, and hide the inside), abstraction (show only what matters), inheritance (reuse and extend a parent class) and polymorphism (one interface, many behaviours).

    BeginnerIn-depth2 min read
  2. 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
  3. What is encapsulation, and how do access modifiers help achieve it?

    Encapsulation means keeping an object's data and the methods that change it together, and hiding the data so outside code can only change it through controlled methods. In Java, access modifiers (private, default, protected, public) decide who can see each member, and making fields private with validated methods is the usual way to encapsulate.

    BeginnerIn-depth3 min read
  4. 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
  5. What are the SOLID principles? Explain each in simple words.

    SOLID is five design guidelines. Single responsibility (one reason to change), Open/closed (add features without editing working code), Liskov substitution (a child must work wherever its parent works), Interface segregation (small, focused interfaces) and Dependency inversion (depend on abstractions, not concrete classes).

    IntermediateIn-depth3 min read
  6. 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
  7. What is the difference between composition and inheritance, and when would you prefer composition?

    Inheritance is an "is-a" relationship where a class extends another and reuses its code. Composition is a "has-a" relationship where a class holds other objects and delegates work to them. Prefer composition by default because it keeps classes loosely coupled and lets you swap behaviour, and use inheritance only for a true, stable "is-a" relationship.

    IntermediateIn-depth3 min read
  8. 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
  9. Is encapsulation the same as data hiding?

    Not exactly. Encapsulation bundles data and the methods that work on it into one unit. Data hiding is the part where the fields are made private so outsiders can only use the safe methods.

    BeginnerQuick answer1 min read
  10. What are the types of inheritance, and which does Java support through classes?

    Single, multilevel, hierarchical, multiple and hybrid. Java supports the first three with classes; multiple inheritance of classes is not allowed, but a class can implement many interfaces.

    BeginnerQuick answer1 min read
  11. What is the difference between a class and an object?

    A class is a blueprint that describes the fields and methods something will have. An object is one real instance built from that blueprint, with its own values stored in memory.

    BeginnerQuick answer1 min read
  12. What is the difference between abstraction and encapsulation?

    Abstraction decides what to show: it exposes a simple view and hides complexity. Encapsulation decides how to protect: it bundles data with methods and restricts access to it.

    BeginnerQuick answer1 min read