Object-Oriented Programming Basics
# CHAPTER 12
Object-Oriented Programming Basics
1. Introduction
Procedural programming (writing functions and variables sequentially) works for small scripts. But when building massive software like an operating system, a banking app, or a video game, procedural code becomes an unmaintainable mess. Object-Oriented Programming (OOP) is a paradigm that organizes software around "Objects" rather than functions. C# is fundamentally an OOP language.2. Learning Objectives
By the end of this chapter, you will be able to:- Define what an Object and a Class are.
- Understand the paradigm shift from Procedural to OOP.
- Identify the 4 Pillars of OOP: Encapsulation, Abstraction, Inheritance, and Polymorphism.
3. Classes vs. Objects
The two most fundamental concepts in OOP are Classes and Objects.- Class (The Blueprint): A class is a template. It defines the properties (data) and behaviors (methods) that a certain entity should have. Think of an architectural blueprint for a house.
-
Object (The Instance): An object is the actual, physical thing built from the blueprint. From one
Houseclass, you can create 100Houseobjects, each painted a different color.
4. Procedural vs. Object-Oriented
*Procedural Way:*As you add more players, this becomes impossible to manage.
*OOP Way:*
We bundle the data and the methods into a single Player class.
The data and logic are encapsulated inside the object. Much cleaner!
5. The 4 Pillars of OOP
Every major OOP language (C#, Java, C++, Python) relies on four core concepts. We will dedicate specific chapters to each of these, but here is the high-level overview:#### Pillar 1: Encapsulation (Data Protection) Encapsulation is the mechanism of hiding the internal state of an object and requiring all interaction to be performed through an object's methods. *Analogy:* You don't directly modify the internal engine valves of your car to speed up. The internal engine is *encapsulated*. You press the gas pedal (a public method).
#### Pillar 2: Abstraction (Simplification) Abstraction means hiding complex implementation details and showing only the essential features to the user. *Analogy:* When you use your smartphone, you press an icon to open an app. You are abstracted away from the millions of lines of machine code executing in the background.
#### Pillar 3: Inheritance (Code Reusability)
Inheritance allows a new class to adopt the properties and methods of an existing class.
*Analogy:* An Animal class might have a Breathe() method. If we create a Dog class and a Cat class, they can *inherit* from Animal. We don't have to rewrite the Breathe() code for every single animal.
#### Pillar 4: Polymorphism (Many Forms)
Polymorphism allows methods to do different things based on the object it is acting upon.
*Analogy:* If you call the Speak() method on a Dog object, it barks. If you call Speak() on a Cat object, it meows. The same method name yields different behaviors.
6. OOP and Memory Explanation
When you define aClass, it takes up almost no memory—it is just a definition.
When you use the new keyword to create an Object (e.g., new Player()), the CLR allocates memory on the Heap for that specific object. The variables pointing to that object live on the Stack. This architecture is why OOP is heavily based on Reference Types.
7. Common Mistakes
- Blurring the line between Class and Object: "I am changing the variable in the Class." No, you change variables in the *Object*. The class is just the blueprint. Changing the blueprint doesn't paint the house.
-
Overcomplicating designs: Beginners often try to create massive, deep inheritance trees (e.g.,
Vehicle -> Car -> SportsCar -> Ferrari). Modern software design favors simpler architectures.
8. Exercises
-
1.
Look around your room. Pick a physical item (e.g., a Television). On paper, design a "Class" for it. List 3 Properties (e.g.,
brand,volume) and 2 Methods (e.g.,TurnOn(),ChangeChannel()).
9. MCQs with Answers
What does OOP stand for?
Which of the following is considered the "Blueprint"?
Which of the following is considered the "Physical Instance" built from the blueprint?
Which OOP pillar involves hiding internal data and protecting it from outside interference?
Which OOP pillar allows a Dog class to receive all the methods of an Animal class automatically?
Which OOP pillar hides complex background logic, providing the user with a simple interface?
Which OOP pillar allows a single method name like Attack() to behave differently depending on whether a Wizard or Warrior calls it?
Where are Objects stored in C# memory?
Which keyword is used in C# to create a new Object from a Class?
Is C# a procedural language or an Object-Oriented language?
10. Interview Questions
- Q: Name the 4 pillars of OOP and give a brief definition of each.
- Q: Explain the difference between a Class and an Object.