Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 12 Beginner

Object-Oriented Programming Basics

Updated: May 17, 2026
5 min read

# 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 House class, you can create 100 House objects, each painted a different color.

4. Procedural vs. Object-Oriented

*Procedural Way:*
csharp
123456
string p1Name = "Arthur";
int p1Health = 100;
string p2Name = "Dragon";
int p2Health = 300;

void TakeDamage(string name, ref int health, int damage) { ... }

As you add more players, this becomes impossible to manage.

*OOP Way:* We bundle the data and the methods into a single Player class.

csharp
1234
Player p1 = new Player("Arthur");
Player p2 = new Player("Dragon");

p1.TakeDamage(50);

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 a Class, 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. 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

Question 1

What does OOP stand for?

Question 2

Which of the following is considered the "Blueprint"?

Question 3

Which of the following is considered the "Physical Instance" built from the blueprint?

Question 4

Which OOP pillar involves hiding internal data and protecting it from outside interference?

Question 5

Which OOP pillar allows a Dog class to receive all the methods of an Animal class automatically?

Question 6

Which OOP pillar hides complex background logic, providing the user with a simple interface?

Question 7

Which OOP pillar allows a single method name like Attack() to behave differently depending on whether a Wizard or Warrior calls it?

Question 8

Where are Objects stored in C# memory?

Question 9

Which keyword is used in C# to create a new Object from a Class?

Question 10

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.

11. Summary

Object-Oriented Programming shifts the focus from writing sequential logic to designing modular Objects that contain their own data and behaviors. The four pillars—Encapsulation, Abstraction, Inheritance, and Polymorphism—form the foundation of enterprise-grade software architecture.

12. Next Chapter Recommendation

Now that you understand the theory, it's time to write the code. In Chapter 13: Classes and Objects, we will build our own classes, create objects, and learn about Fields and Properties.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·