Basic OOPS Concepts In C#

Object-Oriented Programming offers several advantages over the other programming models:

The precise and clear modular approach for programs offers easy understanding and maintenance.

Classes and objects created in the project can be used across the project.

The modular approach allows different modules to exist independently, allowing several developers to work on different modules together.

In this tutorial, we will be focusing more on other major core OOPS concepts:

  • Encapsulation
  • Polymorphism
  • Inheritance
  • Abstraction

Encapsulation

An Object-Oriented Programming concept called encapsulation enables programmers to enclose data and small pieces of code. Using the encapsulation program, you can keep members of one class hidden from members of another class. It's similar to encasing a sensible object in a package. Only pertinent information that is publicly accessible is permitted, and only for those members.

The implementation of encapsulation makes use of access specifiers. In C#, the Access Specifier is used to specify the class member's visibility and accessibility.

The following access specifiers are available in C#.

  • Public
  • Individual
  • Safeguarded
  • Internal

The access specifiers specify the class's visibility and values. It lets you expose data for a specific code section and conceal it from other sections.

  • Public: Anywhere within the project, members can be viewed using the public keyword. The least amount of transparency restriction is on that access specifier.
  • Individual: Only those in the same class can access other members' private data. This is one of the least visible angles.
  • Safeguarded: This member's availability permits access from within and from a different class derived from this one.
  • Internal: Internal accessibility is offered from within. Protected internal is an additional comparable internal accessibility. This permits the same functionality as the internal; the only distinction is that members of this class can be accessed by a child class that inherits it from another project.

Polymorphism

The word polymorphism, which means "one with many forms," comes from Greek. Morph signifies forms, while Poly stands for numerous. It permits numerous implementations of the same class under different names in C#.

There are essentially two components to polymorphism:

Compile-time Transformations

Polymorphism during run-time

  • Polymorphism in Static or Compile Time:

Static polymorphism is another name for compile-time polymorphism. One technique for achieving compile-time polymorphism is method overloading. Since the choice to invoke the method is made during compilation, it is called compile-time polymorphism.

It is accomplished by passing distinct sets of parameters while maintaining the method name.

  • Runtime Polymorphism or Dynamic Polymorphism:

If the name and parameters associated with a method are the same in both the method declaration and the executable, this is known as dynamic polymorphism or runtime polymorphism. A dynamic polymorphism example would be method overriding. The creation of a class that is abstract with limited interface compliance is possible.

Inheritance is used to achieve function overriding. Both the base and derived classes must have an identical name and argument to accomplish method override. No errors are raised since the compiler cannot detect the overriding method during compile time. During runtime, a method's execution is decided.

Inheritance

An essential component of the OOPS idea is inheritance. We construct the child and parent classes in inheritance. The child class inherits all of the parent class's methods, objects, and properties. A kid in a class may also use unique methods for execution.

A base class refers to the parent class, while a derived class refers to a child class that derives the base class.

Abstraction

One of the main tenets of the object-oriented programming style is abstraction. Programmers can hide non-essential features and present only the ones required to the public using abstraction. The Abstract type and interfaces in C# are used to accomplish abstraction.

The "Abstract" keyword can be used to declare a class as abstract. In the hierarchy, the starting class in C# remains the Abstract class. They are distinct from the remainder of the class since they are not instantiated. It is necessary to inherit an abstract C# class.

Interface

An interface in C# is a class's blueprint. The interface is used to achieve 100% abstraction and is comparable to an abstract class. By default, every method that is described within the interface is abstract. It cannot be created and lacks a method body.

The primary goals of the interface are complete abstraction and multiple inheritance. The class or struct that implements each method signature defined inside the interface's declaration should supply its implementation.