CHAPTER 02
Intermediate
Object-Oriented Programming Fundamentals
Updated: May 16, 2026
25 min read
# CHAPTER 2
Object-Oriented Programming Fundamentals
1. Introduction
Design patterns are almost exclusively built upon the principles of Object-Oriented Programming (OOP). If you do not intimately understand classes, interfaces, and polymorphism, attempting to learn the Abstract Factory or Observer patterns will be an exercise in frustration. OOP is a programming paradigm based on the concept of "objects," which can contain data (attributes) and code (methods). In this chapter, we will master the foundational pillars of OOP. We will build classes, hide data using encapsulation, share logic via inheritance, and unlock the true power of design patterns through polymorphism and abstraction.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the difference between a Class and an Object.
- Implement Encapsulation to protect internal object state.
- Utilize Inheritance to share code between related classes.
- Understand Abstraction and the use of Interfaces/Abstract Classes.
- Master Polymorphism to write flexible, interchangeable code.
3. Classes and Objects
The foundation of OOP is modeling real-world concepts.-
The Class (The Blueprint): A class is a template. It defines what properties and methods an entity *will* have. (e.g., A
Carclass defines that all cars will have acolorand astartEngine()method).
-
The Object (The Instance): An object is a specific, physical realization of a class. (e.g.,
myHondais an object created from theCarclass. Its specific color is "Red").
4. Encapsulation (Data Hiding)
Encapsulation is the practice of restricting direct access to an object's data, and bundling the data with the methods that operate on it.- The Concept: You should not be able to directly change the balance of a Bank Account variable from outside the class. You must go through a secure method.
- Access Modifiers:
-
public: Accessible from anywhere.
-
private: Accessible ONLY from inside the class itself.
-
protected: Accessible from the class and its children (subclasses).
5. Inheritance
Inheritance allows a new class (Child) to inherit the properties and methods of an existing class (Parent).-
The Benefit: Code reuse. If a
Dogand aCatboth share 90% of the same logic (eating, sleeping), you create anAnimalparent class. TheDogclass *extends*Animal, getting all that code for free, and then adds its specificbark()method.
6. Abstraction and Polymorphism
These two concepts are the absolute core of all design patterns.-
Abstraction: Hiding complex implementation details and showing only the essential features. This is achieved using Interfaces or Abstract Classes. An interface defines a *contract* (e.g., "Any class that implements
PaymentMethodMUST have apay()method").
-
Polymorphism ("Many Forms"): The ability of different classes to be treated as instances of the same class through a common interface. If
CreditCardandPayPalboth implement thePaymentMethodinterface, your checkout code doesn't care which one the user selected; it just callspay(), and the correct specific logic executes.
7. Diagrams/Visual Suggestions
*OOP Diagram: Inheritance and Polymorphism*
text
8. Code Examples
Let's look at Polymorphism in action using PHP.
php
9. Best Practices
-
Favor Composition Over Inheritance: A massive mistake in OOP is building massive, deeply nested inheritance trees (e.g.,
CarextendsVehicleextendsMachineextendsObject). This makes code rigid and fragile. Instead, use Composition: build small classes and *inject* them into other classes. (e.g., ACar*has an*Engine, rather than aCar*is a*Machine).
10. Mini Project: Build an Animal Hierarchy
-
1.
Create an abstract class
Animalwith a private propertynameand an abstract methodmakeSound().
-
2.
Create two subclasses:
DogandCat.
-
3.
Implement the
makeSound()method differently for both (WoofvsMeow).
-
4.
Write a loop that iterates through an array of
Animalobjects, callingmakeSound()on each, demonstrating polymorphism.
11. Practice Exercises
-
1.
Define Encapsulation. Why is making class properties
publicgenerally considered bad practice in enterprise software?
- 2. Explain Polymorphism in your own words. How does an Interface enable Polymorphism?
12. MCQs with Answers
Question 1
Which OOP principle is focused on hiding the internal state of an object and requiring all interaction to be performed through an object's methods (getters/setters)?
Question 2
You have an Interface called Notification with a method send(). The classes EmailNotification and SMSNotification both implement this interface. If a function accepts the Notification interface as a parameter, allowing it to work seamlessly with both Email and SMS classes, which OOP principle is being utilized?
13. Interview Questions
- Q: Explain the difference between an Abstract Class and an Interface. In what scenario would you choose to use one over the other?
- Q: Walk me through the concept of "Favor Composition over Inheritance." Why are deep inheritance hierarchies considered an anti-pattern in modern OOP?
-
Q: Describe Encapsulation. What are access modifiers (
public,private,protected), and how do they enforce data hiding?