DEV Community

Cover image for C# OOP
Zafar Urakov
Zafar Urakov

Posted on

C# OOP

Introdaction to OOP

OOP stands for "Object-Oriented Programming." It is an approach to writing programs that is based on objects rather than functions and procedures. This model focuses on objects, not actions, and data, not logic. An object is an instance of a class. All implementations of the same class are similar to each other but can have different parameters and values. Objects can utilize methods that are specific to them.

OOP greatly simplifies the process of organizing and creating the structure of a program. Separate objects that can be modified without affecting other parts of the program also simplify making changes to the program. As programs become larger over time and their maintenance becomes more challenging, these two aspects of OOP become increasingly relevant.

What are the principles of OOP?

Abstraction

Abstraction is the first and perhaps the most important principle of Object-Oriented Programming (OOP). It allows us to hide the implementation details of an object and provide only the necessary interface for working with it. In other words, abstraction enables us to focus on what an object does rather than how it does it.
For example:

abstract class Bird
{
    public abstract void MakeSound();

    public void sleep() => 
      Console.WriteLine("Zzz");
}

class Sparrow : Bird
{
    public override void MakeSound()
    {
        Console.WriteLine("Chirp chirp!");
    }
}

class Program
{
  static void Main(string[] args)
  {
    Sparrow sparrow = new Sparrow();
    sparrow.MakeSound();
    sparrow.sleep(); 
  }
}
Enter fullscreen mode Exit fullscreen mode

Encapsulation

Encapsulation is a principle that allows hiding the internal details of an object's implementation and providing limited access to them. This makes the code more secure and maintainable.
For example:

class BankAccount
{
    private decimal balance;

    public void Deposit(decimal amount)
    {
        if (amount > 0)
            balance += amount;
    }

    public decimal GetBalance()
    {
        return balance;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, balance is a private field, and access to it is managed through the methods Deposit and GetBalance. This allows for controlling changes to the account balance and ensuring safe access to its value.

Polymorphism

Polymorphism is the ability of objects from different classes to respond to the same methods. This allows us to write flexible and versatile code that can work with different types of objects.
For example:

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

class Rectangle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a rectangle");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, Shape is the base class, and Circle and Rectangle are its derived classes. When the Draw() method is called on objects of different types, the corresponding method from each class will be invoked.

Inheritance

Inheritance is the ability to create a new class based on an existing class, inheriting its properties and methods. This allows for code reuse and the creation of class hierarchies.
For example:

using System;

class Animal
{
    public void Eat()
    {
        Console.WriteLine("The animal is eating.");
    }

    public void Sleep()
    {
        Console.WriteLine("The animal is sleeping.");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("The dog is barking.");
    }
}

class Program
{
    static void Main()
    {
        Dog myDog = new Dog();

        myDog.Eat();    // Inherited from Animal
        myDog.Sleep();  // Inherited from Animal

        myDog.Bark();   // Defined in Dog

    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have two classes: Animal and Dog. Dog is a subclass of Animal, which means it inherits the Eat and Sleep methods from the Animal class. Additionally, it has its own method, Bark.

In the Main method, we create an instance of the Dog class (myDog) and demonstrate how we can call methods from both the base class (Animal) and the derived class (Dog). This illustrates the concept of inheritance in C#.

Conclusion

The principles of object-oriented programming are a powerful tool for creating clean, flexible, and understandable code. Abstraction, encapsulation, polymorphism, and inheritance help in developing programs that are easy to maintain and extend. I hope this article has helped you better understand these principles and how they are applied in C#.

I have a lot of interesting code on GitHun

Top comments (0)