OOP

Object-oriented programming (OOP) in C# is a programming paradigm that uses “objects” to model real-world entities. Objects are instances of classes, which can contain data in the form of fields (also called attributes or properties) and code in the form of methods (also known as functions). C# supports the four fundamental principles of OOP: encapsulation, inheritance, polymorphism, and abstraction.

Four Principles of OOP

  1. Encapsulation: Encapsulation is the concept of wrapping data and methods that operate on the data within a single unit, or class. This hides the internal state of the object and protects it from unauthorized access and modification.
  2. Inheritance: Inheritance allows a class (called a derived class or child class) to inherit fields and methods from another class (called a base class or parent class). This promotes code reuse and establishes a hierarchical relationship between classes.
  3. Polymorphism: Polymorphism allows methods to do different things based on the object they are acting upon. It enables one interface to be used for a general class of actions, with the specific action determined by the exact nature of the situation.
  4. Abstraction: Abstraction simplifies complex systems by modeling classes appropriate to the problem and working at the most relevant level of inheritance for a particular aspect of the problem.

Example

using System;

namespace OOPExample
{
  // Base class
  public class Animal
  {
      // Encapsulation: private field
      private string name;

        // Encapsulation: public property
      public string Name
      {
          get { return name; }
          set { name = value; }
      }

        // Encapsulation: method
      public virtual void Speak()
      {
          Console.WriteLine("The animal makes a sound.");
      }
  }

    // Derived class inheriting from Animal
  public class Dog : Animal
  {

        // Method overriding (Polymorphism)
      public override void Speak()
      {
          Console.WriteLine("The dog barks.");
      }
  }

    // Derived class inheriting from Animal
  public class Cat : Animal
  {
      // Method overriding (Polymorphism)
      public override void Speak()
      {
          Console.WriteLine("The cat meows.");
      }
  }

  class Program
  {
      static void Main(string[] args)
      {

            // Creating objects of derived classes
          Dog myDog = new Dog();
          myDog.Name = "Buddy";
          Console.WriteLine(myDog.Name);

            myDog.Speak(); // Outputs: The dog barks.

          Cat myCat = new Cat();
          myCat.Name = "Whiskers";
          Console.WriteLine(myCat.Name);

            myCat.Speak(); // Outputs: The cat meows.

            // Demonstrating polymorphism
          Animal myAnimal1 = new Dog();
          Animal myAnimal2 = new Cat();
          myAnimal1.Speak(); // Outputs: The dog barks.
          myAnimal2.Speak(); // Outputs: The cat meows.
      }
  }
}

Explanation of the Example

  1. Encapsulation:
    • The Animal class encapsulates the name field and exposes it through the Name property.
    • The Speak method in Animal encapsulates the behavior of making a sound.
  2. Inheritance:
    • The Dog and Cat classes inherit from the Animal class, gaining access to the Name property and the Speak method.
  3. Polymorphism:
    • The Speak method is overridden in the Dog and Cat classes to provide specific behavior.
    • At runtime, the appropriate Speak method is called based on the actual object type.
  4. Abstraction:
    • The Animal class abstracts the common properties and methods that all animals share, while Dog and Cat provide specific implementations.
Post a comment

Leave a Comment

Scroll to Top