Object-Oriented Programming

Explain and apply encapsulation, abstraction, inheritance and polymorphism with code, plus the OOP questions asked in almost every fresher interview.

Beginner · about 8 hours · difficulty 2/5

Learn first: How Campus and Off-Campus Hiring Works

OOP is the most asked theory topic in fresher interviews, especially at service companies. Expect definitions, small code examples and "what is the output" questions in your chosen language.

The four pillars

  • Encapsulation: bundle data with methods and hide it behind access modifiers (private, protected, public).
  • Abstraction: expose what an object does, hide how. Abstract classes and interfaces.
  • Inheritance: reuse and extend behaviour. Types: single, multilevel, hierarchical, multiple (via interfaces in Java), and the diamond problem (virtual inheritance in C++).
  • Polymorphism: compile-time (method overloading, operator overloading in C++) and runtime (method overriding through virtual functions or dynamic dispatch).

Frequently asked questions

  • Class vs object, constructor types (default, parameterised, copy), destructors.
  • Abstract class vs interface, and when to use each.
  • Overloading vs overriding.
  • static and final (Java), virtual functions and vtables (C++), this and super.
  • Composition vs inheritance; "has-a" vs "is-a".
  • Shallow vs deep copy.
  • A short intro to SOLID principles and one or two design patterns (Singleton, Factory, Observer) for product-company rounds.

Pitfalls

  • Reciting textbook definitions without a code example. Always back each pillar with a 3 to 5 line snippet.
  • Confusing abstraction with encapsulation.

You are ready to move on when you can explain all four pillars with your own code examples and answer overloading vs overriding with output prediction.

Key concepts

  • Classes, objects, constructors
  • Encapsulation and access modifiers
  • Abstraction: abstract classes and interfaces
  • Inheritance types and diamond problem
  • Overloading vs overriding
  • Virtual functions and dynamic dispatch
  • Composition vs inheritance
  • SOLID and common design patterns

Mini project

Build a small console Library Management system in your language with Book, Member and Librarian classes, an abstract LibraryItem, an interface for borrowable items, overloading and overriding examples, and a README mapping each OOP pillar to a line of code.

Check yourself

What is the difference between an abstract class and an interface, and when would you choose each?

Free resources