Object Oriented Programming

Lesson 3.1: Introduction to OOP Principles


In this lesson, you'll discover a powerful way to organize and structure your C# code: Object-Oriented Programming. OOP is a popular paradigm that helps you model real-world entities and their interactions within your programs.


Key OOP Concepts


Classes:

A class is a blueprint or template for creating objects. It defines the properties (data) and methods (behaviors) that objects of that class will have.

Think of a class as a cookie cutter – it defines the shape and characteristics of the cookies (objects) you can create from it.

Objects:

An object is an instance of a class. It represents a specific entity that has the properties and methods defined by its class.

Using the cookie cutter analogy, each cookie you make is an object.

Encapsulation:

Encapsulation means bundling data (properties) and the functions (methods) that operate on that data into a single unit (the class).

This helps hide the internal details of an object and provides a controlled interface for interacting with it.

Inheritance:

Inheritance allows you to create new classes (derived classes) that inherit properties and methods from existing classes (base classes).

This promotes code reuse and creates a hierarchical relationship between classes.

Polymorphism:

Polymorphism means "many forms." In OOP, it refers to the ability of objects of different classes to respond to the same method call in their own way.

Think of it like asking different animals to "speak" – each animal will make its own unique sound.

Example: A Simple Class and Object


C#

class Dog

{

    // Properties (data)

    public string Name { get; set; } // The dog's name

    public string Breed { get; set; } // The dog's breed


    // Method (behavior)

    public void Bark() 

    {

        Console.WriteLine("Woof!");

    }

}


class Program

{

    static void Main(string[] args)

    {

        // Create a Dog object (an instance of the Dog class)

        Dog myDog = new Dog(); 

        myDog.Name = "Fido";

        myDog.Breed = "Golden Retriever";


        // Call the Bark method

        myDog.Bark(); // Output: Woof!

    }

}

In this example:


We define a Dog class with properties for Name and Breed and a method called Bark().

In the Main method, we create a Dog object, set its properties, and call the Bark() method.

Why OOP?


Modularity: OOP helps you break down complex programs into smaller, more manageable components (classes).

Code Reusability: Inheritance allows you to reuse code from base classes in derived classes.

Flexibility: Polymorphism enables objects to respond to the same message in different ways.

Maintainability: Encapsulation makes your code easier to understand and maintain.

Coming Up...


In the following lessons, you'll explore these OOP principles in more depth and learn how to apply them to build real-world applications. We'll cover:


Creating classes and objects

Constructors and destructors

Inheritance and polymorphism

Abstract classes and interfaces

Let me know if you have any other questions.