Module 2: Basic Concepts of OOP
Subject: Object-Oriented Programming
Detailed guide to Object-Oriented Programming (OOP) concepts: Classes, Objects, Encapsulation, Abstraction, Inheritance, Polymorphism, Virtual Functions, and C++ vs Java pitfalls.
Concept Summary
Key Revision Rules & Formulas
- Classes are blueprints taking 0 bytes; Objects are memory-instantiated instances.
- Encapsulation achieves Data Hiding using private access specifiers and getter/setter methods.
- Abstraction hides implementation details using Abstract Classes and Interfaces.
- Inheritance establishes an 'is-a' relationship for code reusability.
- Java does support Multilevel inheritance, but does not support Multiple inheritance for classes to avoid the Diamond Problem.
- Diamond Problem in C++ is resolved using virtual base class inheritance ('virtual public Base').
- Function Overloading (same name, different arguments) is Compile-Time / Static Polymorphism.
- Method Overriding (same name & signature in base/derived classes) is Run-Time / Dynamic Polymorphism.
- Virtual Functions enable dynamic dispatch at runtime using VTable and VPtr mechanism.
- Abstract Classes contain at least one Pure Virtual Function (= 0) and cannot be instantiated.
- Destructors in Base classes must be declared 'virtual' to ensure proper cleanup of derived objects and avoid memory leaks.
- Composition represents strong ownership ('part-of') where child objects die with parent.
Common Exam Pitfalls
- Confusing Encapsulation (how data is hidden via access specifiers) with Abstraction (what features are exposed via interfaces).
- Assuming constructors have a 'void' return type; constructors have NO return type at all.
- Forgetting to declare a Base Class Destructor as 'virtual' in C++, leading to incomplete destruction of derived objects and memory leaks.
- Confusing Method Overloading (compile-time, same scope, different signature) with Method Overriding (run-time, inherited scope, identical signature).
- Attempting to instantiate an Abstract Class or Interface directly using the 'new' keyword.
- Believing C++ private members are inherited by child classes; private members are NOT accessible in derived classes.
- Confusing Aggregation (weak 'has-a', independent lifetimes) with Composition (strong 'part-of', bound lifetimes).
- Assuming 'static' member variables are created per object; static members are shared globally across ALL instances of a class.
- Assuming Java doesn't support multilevel inheritance; Java supports multilevel (A->B->C) but not multiple class inheritance.
- Failing to catch exceptions by reference in C++, causing object slicing during exception handling.
- Object slicing in C++ when passing derived objects by value to a base class parameter.
Sample Practice Questions
Question 1: What is the primary difference between a Class and an Object in Object-Oriented Programming?
- A class occupies memory while an object is a concept
- A class is a blueprint/template while an object is a runtime instance occupying memory
- A class can have functions while an object can only have variables
- Classes are used in C while objects are used in SQL
Explanation: A Class is a user-defined blueprint (takes 0 bytes), while an Object is a physical runtime instance of that class allocated in memory.
Question 2: Which OOP principle restricts direct access to an object's internal components and bundles data with methods?
- Abstraction
- Inheritance
- Encapsulation
- Polymorphism
Explanation: Encapsulation bundles fields and methods into a single class unit and achieves Data Hiding using private/protected access specifiers.
Question 3: Which access specifier makes class members accessible ONLY within member functions of the defining class itself?
- public
- protected
- private
- default
Explanation: Private members cannot be accessed outside the defining class, not even by derived child classes.
Question 4: What is the return type of a Constructor in C++/Java?
- void
- int
- Object
- Constructors have no return type
Explanation: Constructors are special functions called automatically on object creation and have NO return type (not even void).
Question 5: Which type of constructor initializes an object using another existing object of the same class?
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Virtual Constructor
Explanation: A Copy Constructor initializes a new object using an existing object of the same class (e.g., ClassName(const ClassName &obj)).
Question 6: In what order are constructors executed in a derived class inheritance chain?
- Derived class constructor -> Base class constructor
- Base class constructor -> Derived class constructor
- Both execute simultaneously
- Random order decided by runtime
Explanation: Construction flows top-down: Base Class Constructor executes first to initialize base members, followed by Derived Class Constructor.
Question 7: In what order are destructors executed when a derived class object is destroyed?
- Derived class destructor -> Base class destructor
- Base class destructor -> Derived class destructor
- Only Base class destructor executes
- Only Derived class destructor executes
Explanation: Destruction flows bottom-up in exact reverse order of construction: Derived Destructor executes first, then Base Destructor.
Question 8: What is the scope and lifetime of a 'static' member variable in a class?
- Created per object and destroyed when object goes out of scope
- Shared globally across ALL instances of the class and initialized once
- Accessible only inside constructors
- Allocated on the call stack
Explanation: Static member variables belong to the class itself rather than individual objects, sharing a single memory location across all instances.
Question 9: What is the purpose of the implicit 'this' pointer inside a class member function?
- Points to the static class definition
- Points to the parent base class
- Points to the invoking object instance
- Points to the global namespace
Explanation: The 'this' pointer is an implicit parameter passed to non-static member functions holding the memory address of the invoking object.
Question 10: Which C++ feature allows a non-member function to access private and protected members of a class?
- Virtual Function
- Inline Function
- Friend Function
- Static Function
Explanation: A Friend Function (declared with friend keyword) is granted explicit privilege to access private/protected members of a class.