C# OOPS (Object Oriented Programming) Concepts with Examples

  By : Suresh Dasari
  Posted On : 21-May-2023

Here, we will learn what is object-oriented programming (oops) in c#, uses of object oriented programming (oops) in c#, learn oops (object oriented programming) concepts such as abstraction, encapsulation, inheritance, and polymorphism in c# with examples.

What is Object Oriented Programming (OOPs) in C#?

In c#, object-oriented programming is a strategy that will provide principles to work with the objects while developing the applications. Object-oriented programming will allow users to create objects which are instances of classes and allow them to create methods to operate on those objects.  Generally, in any programming language object-oriented programming is commonly called OOPS.

 

C# is a fully object-oriented programming language and it provides support for OOPS concepts. If we build applications using object-oriented programming it will help us to improve the code reusability and maintainability.

 

The following are some basic principles of object-oriented programming to build object-oriented applications.

 

  • Classes and Objects in C#
  • Abstraction in C#
  • Encapsulation in C#
  • Inheritance in C#
  • Polymorphism in C#

Classes and Objects in C#

In c#, class is a data structure and it is helpful to combine various types of data members such as fields, properties, methods, events, etc. into a single unit.

 

To define a class, we need to use the class keyword. Following is an example of defining the class in c#.

 

Public class Employees {
   // Properties, fields, methods, etc.
}

In c#, the object is an instance of a class, and it is useful to access the class members and member functions. Following is the example of creating the object of the Employees class.

 

Employees emp = new Employees();

By using the emp object, we can access all the Employees class data members and member functions.

 

To learn more about classes and objects, visit Classes and Objects in C# with Examples.

Abstraction in C#

In c#, abstraction is one of the basic principles of object-oriented programming. The abstraction is useful to hide the implementation details and expose only the essential features of an object.

In c#, we can implement abstraction functionality by using abstract classes and interfaces.

 

To learn more about abstraction, visit Abstraction in C# with Examples.

Encapsulation in C#

In c#, encapsulation is a process of binding data members, member functions into a single unit. By using encapsulation, you can restrict the access of particular members of the class to the outside and allow access to the specific members of an object based on your requirements.

 

In c#, you can control the accessibility of class members using the following access modifiers.

 

  • Public
  • Private
  • Protected
  • Internal
  • Protected Internal

The access modifiers are useful to control the visibility of class properties and methods based on our requirements.

 

To learn more about encapsulation, visit Encapsulation in C# with Examples.

Inheritance in C#

In c#, inheritance is one of the basic principle of object oriented programming. It is useful to create a new class (derived or child class) by inheriting all the properties and methods from another class (base class) to reuse, extend, or modify the functionality of the class based on our requirements.

 

Following is the example of implementing the inheritance in c#.

 

public class User
{
   public void GetDetails()
   {
      // Method implementation
   }
}
public class Salary: User
{
   //Your class implementation
}

 The c# will support only single inheritance which means a class can only inherit from a single base class. To learn more about inheritance, visit Inheritance in C# with Examples.

Polymorphism in C#

In c#, polymorphism means the ability to take more than one form and it is one of the main concepts of object-oriented programming.

 

The polymorphism will provide the ability for the classes to implement different methods with the same name. Following is the example of implementing the polymorphism in c#.

 

public class MathOperations
{
   public void Addition(int a, int b)
   {
      Console.WriteLine("a + b = {0}", a + b);
   }
   public void Addition(int a, int b, int c)
   {
      Console.WriteLine("a + b + c = {0}", a + b + c);
   }
}

In c#, you can achieve polymorphism using method overloading and method overriding. To learn more about polymorphism, visit Polymorphism in C# with Examples.

 

These are some of the fundamental concepts of object-oriented programming in c# to build reusable, highly scalable applications. To learn more about object-oriented programming concepts, visit C# Tutorial.