Lesson 25 / 25

Revision and Interview Questions

Recall OOP concepts quickly for exams, vivas and interviews.

Cheat sheet

Basics: class (blueprint) vs object (instance); state, behaviour, identity; references; this/self; constructors, overloading, chaining with this(...); static members belong to the class. Encapsulation: private state, public operations, invariants; access modifiers (private, package, protected, public); immutability with final fields, no setters, defensive copies, records. Abstraction: expose what, hide how; abstract classes (state + partial implementation, single inheritance) vs interfaces (contracts, multiple, default methods). Inheritance: is-a; super(...) first; overriding rules (same signature, covariant return, no narrower access, @Override); fragile base class; diamond problem (C++ virtual inheritance, Python MRO, Java default-method conflicts). Polymorphism: compile-time overloading vs run-time overriding with dynamic dispatch; upcasting safe, downcasting needs checks; programming to interfaces; duck typing. Relationships: association, aggregation (hollow diamond), composition (filled diamond), dependency; composition over inheritance. SOLID: SRP, OCP, LSP, ISP, DIP; dependency injection. Objects: identity vs equality; equals/hashCode contract; GC; try-with-resources; shallow vs deep copy; contracts and exceptions.

Common exam and interview questions

Answer each with a definition and a short code example.

1. Explain the four pillars of OOP with real-world examples.
2. Differentiate between a class and an object.
3. What is the difference between overloading and overriding?
4. Abstract class vs interface: when would you use each?
5. Why does Java not support multiple inheritance of classes? How are interfaces different?
6. What is dynamic dispatch? How does C++ implement it?
7. Explain association, aggregation and composition with UML notation.
8. What does "favour composition over inheritance" mean? Give an example.
9. State the SOLID principles and give an example of an LSP violation.
10. Why must hashCode be overridden whenever equals is overridden?
11. How do you make a class immutable in Java?
12. Design the classes for a library management system.

Use one running example

In a viva or interview, illustrate every concept with the same small domain, such as bank accounts or shapes. It shows the ideas fit together and saves time inventing new examples.

Quick check: Which concept allows a superclass reference to call the subclass's version of a method at run time?

  • Method overloading
  • Encapsulation
  • Static binding of fields
  • Run-time polymorphism through overriding (dynamic dispatch)
Answer

Run-time polymorphism through overriding (dynamic dispatch) — Dynamic dispatch chooses the overriding method of the actual object at run time.