Back to Curriculum

Object-Oriented Programming

📚 Lesson 5 of 15 ⏱️ 50 min

Object-Oriented Programming

50 min

C# is a fully object-oriented language supporting classes, inheritance, polymorphism, and encapsulation, enabling you to model real-world entities and relationships. OOP principles help organize code, promote reuse, and make applications maintainable. Understanding OOP enables building complex applications. OOP is fundamental to C# programming.

Classes can have fields (data), properties (controlled access to data), methods (behavior), and constructors (initialization). Fields store data directly. Properties provide controlled access with getters and setters. Methods define behavior. Constructors initialize objects. Understanding class members enables effective class design. Classes are blueprints for objects.

Properties provide controlled access to class members with getters and setters, enabling validation, computation, and encapsulation. Auto-properties (public int Age { get; set; }) automatically create backing fields. Custom properties enable validation and computed values. Understanding properties enables proper encapsulation. Properties are preferred over public fields.

C# supports single inheritance (one base class) but multiple interface implementation, providing a balance between flexibility and simplicity. Inheritance enables code reuse and polymorphism. Interfaces define contracts without implementation. Understanding inheritance and interfaces enables effective OOP design. Inheritance and interfaces are core OOP concepts.

Encapsulation hides internal implementation details and exposes only what's necessary through public members. Access modifiers (public, private, protected, internal) control visibility. Private members are hidden from outside the class. Understanding encapsulation enables secure, maintainable code. Encapsulation is a fundamental OOP principle.

Best practices include using properties instead of public fields, keeping classes focused (single responsibility), using meaningful names, documenting public APIs, and following SOLID principles. Understanding OOP enables building maintainable applications. OOP is essential for complex applications.

Key Concepts

  • C# supports classes, inheritance, polymorphism, and encapsulation.
  • Classes have fields, properties, methods, and constructors.
  • Properties provide controlled access with getters and setters.
  • C# supports single inheritance but multiple interface implementation.
  • Encapsulation hides implementation details.

Learning Objectives

Master

  • Creating classes with fields, properties, and methods
  • Understanding inheritance and polymorphism
  • Implementing interfaces
  • Using access modifiers for encapsulation

Develop

  • Understanding OOP principles
  • Designing maintainable class hierarchies
  • Appreciating OOP's role in software design

Tips

  • Use properties instead of public fields for encapsulation.
  • Keep classes focused on a single responsibility.
  • Use meaningful names for classes, properties, and methods.
  • Document public APIs with XML comments.

Common Pitfalls

  • Using public fields instead of properties, breaking encapsulation.
  • Creating classes that do too much, violating single responsibility.
  • Not understanding inheritance, creating tight coupling.
  • Not using interfaces, missing flexibility opportunities.

Summary

  • C# is fully object-oriented with classes, inheritance, and polymorphism.
  • Properties provide controlled access to class members.
  • C# supports single inheritance and multiple interface implementation.
  • Encapsulation hides implementation details.
  • Understanding OOP enables maintainable applications.

Exercise

Create a simple class hierarchy with inheritance and polymorphism.

using System;

// Base class
public class Animal
{
    public string Name { get; set; }
    
    public Animal(string name)
    {
        Name = name;
    }
    
    public virtual void MakeSound()
    {
        Console.WriteLine("Some animal sound");
    }
}

// Derived class
public class Dog : Animal
{
    public Dog(string name) : base(name) { }
    
    public override void MakeSound()
    {
        Console.WriteLine($"{Name} says: Woof!");
    }
}

public class Cat : Animal
{
    public Cat(string name) : base(name) { }
    
    public override void MakeSound()
    {
        Console.WriteLine($"{Name} says: Meow!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal[] animals = {
            new Dog("Buddy"),
            new Cat("Whiskers"),
            new Animal("Unknown")
        };
        
        foreach (var animal in animals)
        {
            animal.MakeSound();
        }
    }
}

Code Editor

Output