19th Mar 2021

Pillars Of OOPS In C#

.NET

Written By, Manharsinh Jadeja

Overview

In this blog, we are going to discuss the pillars of OOPS in C#. There are many blogs, articles available on the internet regarding pillars of oops but in this particular blog I will try to explain to you with simple and realistic examples so you can get a clear idea of the pillars of OOPS in C#.

So before we start I want to convey one thing to you guys that at the end of this blog you will have a clear mindset of four pillars with real-life and coding examples.

So let’s begin.

Introduction

OOP stands for Object-Oriented Programing!

Pillars of OOPS in C#! So first we have to understand what OOPS is,  and how it differs from procedural programming. In Procedural programming, we are applying methods and procedures to data while OOP is about creating objects that can contain data as well as methods.

Procedural programming is based on an unreal world. Object-oriented programming is based on the real world. Like at starting of our coding life we learn logic building through procedural programming like C Language while for real-world implementation we are using OOP like C#, Java, etc.

How many Pillars?

pillars-of-c-sharp-pillars

Figure 1.2

There are Four Pillars,

  1. Inheritance
  2. Encapsulation
  3. Abstraction
  4. Polymorphism
  1. Inheritance:

    pillars-of-c-sharp-inheritance

    Figure 1.3

    Simply we can say it is a mechanism in which a child object acquires all property of the parent object. As a real-life example, we can say we are the child class and we will access all properties of our parents this is the inheritance.
    There are 3 types of Inheritance,

    1. Single Inheritance
    2. Multi-level Inheritance
    3. Multiple Inheritance
    1. Single Inheritance
      When one class inherits another class at that time we can say it is single inheritance.

      pillars-of-c-sharp-single-inheritance

      Figure 1.4

      In the above image, you can see A is parents class and B is child class so as per definition you can compare this example like B is accessing the property of its parents class and that is A.

      Let see the coding example of the Inherited field as well as method,

      pillars-of-c-sharp-single-inheritance-code-1

      Figure 1.5

      pillars-of-c-sharp-single-inheritance-code-2

      Figure 1.

      In figure 1.5 you can see we have one parent class(Employee) with one public specifier field salary and one Child class(Programmer) with a public specifier field bonus. I hope you know that whenever we print parent and child class first it will print parent and then it will print child. So hear from the main class when we try to access fields by making the object of child class so first, it will print the field of parents and at the last, it will print the field of child.

      Now in figure 1.6, you can see we have one parent class(Animal) with one public method eat and one child class(Dog) with public method bark. so similar to figure 1.5 when we try to access through the object of child(Dog) class first it will print data from parents method eat and at the last, it will print the child method bark.

    2. Multi-Level Inheritance
      When one class inherits another and it also inherits by another at that time we can say it is Multilevel inheritance.

      pillars-of-c-sharp-multilevel-inheritance

      Figure 1.7

      In the above example, you can see that C is accessing the property of B and B is also accessing the property of A. As a real-life example, we can say the grandchild is accessing the property of its parent class and the parent is accessing the property of its parents so we can say it is an example of Multilevel inheritance.

      pillars-of-c-sharp-multilevel-inheritance-code

      pillars-of-c-sharp-multilevel-inheritance-code-output

      Figure 1.8

      As per figure 1.8, you can see BabyDog is accessing the property of its parent class means Dog, and Dog is also accessing the property of its parent class means Animal. So when we try to access the class through making grandchild(BabyDog) object first it will print public method of grandparents(eat) then it will print public method of parents(bark) and at the last, it will print the public method of itself(weep).

    3. Multiple Inheritance
      When one subclass or child class is accessing the property of its multiple parent or superclass at that time we can say it is multiple inheritance.

      pillars-of-c-sharp-multiple-inheritance

      Figure 1.9

      In the above example, you can see C is a child class or subclass and that is the accessing property of its parent class or superclass. As a real-life example, we can say we are accessing the property of our mom and dad means a single child accessing property of multiple classes. so it is an example of multilevel inheritance.

      We will three-questions regarding Multiple Inheritance so you will get a clear idea. Questions you can see in below image,

      pillars-of-c-sharp-multiple-inheritance-image

      Figure 2.0

      1. .NET Support Multiple Inheritance ? – .NET is not supporting Multiple Inheritance!
      2. What will happen if i write a Multiple Class Inheritance Using C#? – When we write Multiple Inheritance in c# it will give a warning message during our code execution program and the warning message will be “Expected an Inheritance”.

        pillars-of-c-sharp-multiple-inheritance-code

        Figure 2.1

        From figure 2.1, you can see HoseOwnerClass is trying to access the property of both parent class GuestVist and FriendsandRelationClass. So it will give the warning ‘Expected an Interface’.

      3. Then how can we use Multiple Inheritance? – Using Interface you can use Multiple-inheritance in C#.

      To know more about Interface you can refer to this website : C# Interface

  2. Encapsulation
    Simply we can say Encapsulation is a process of binding data members and data function together. or in another sense, we can say it is a process of binding variables, properties, and methods together.

    pillars-of-c-sharp-encapsulation

    Source

    Let’s Understand encapsulation with a real-life simple example like capsule is one of the examples of encapsulation. Like in Capsule it binds all chemical contents required for curing specific disease together just like the class which binds data members and member functions. Let’s understand through one coding example what will happen if we did not use encapsulation or we can say how it will help us to hide data.

    pillars-of-c-sharp-encapsulation-code-1

    Figure 2.2

    In figure 2.2, you can see we have one class Account with one private field balance and with one regular method. So here we restricted data balance using private access specifiers means it can access through its class itself. So when some user will try to initialize the value of balance by making an object so it will throw the error because for data security here we give data members private so he or she can not change
    data. But now just imagine if we did not use encapsulation then any user can change the value of balance so by changing the value of balance we can also be the richest person in the world if we want to be because we haven’t used data security or we can say we haven’t use encapsulation for making some data private.
    Now the question is if some of the time I want to change data members after initializing as a private how I can? – So the answer is using the get and set method we can access private data
    members also using this method you can give specific rights like read-only or write-only or both. Let’s understand through coding example,

    pillars-of-c-sharp-encapsulation-code-2

    Figure 2.3

    From Figure 2.3, you can see the department name is private but I want to take that data from the user. Now I can access the private access specifiers using the get and set method. So in this example, you can see in the get method I am returning the name of the department, and while in the set method I am initializing the new value of the department name. if we use the only get method means it will just return the value so we can say that is read-only rights like users can just see the name of the department but can’t initialize value while if we use set only it will just set the value so we can say it is write-only rights.

  3. Abstraction
    Simply abstraction is a process of hiding unnecessary things from users or we can say just show the necessary things. In a proper definition manner, we can say Abstraction is the process of showing only essential/necessary features of an entity/object to the outside world and hide the other irrelevant information.

    pillars-of-c-sharp-abstraction

    Figure 2.4

    Essential Things For Car-Users :

    pillars-of-c-sharp-encapsulation-image-1Figure 2.5

    Irrelevant Things For Car-Users :

    pillars-of-c-sharp-encapsulation-image-2Figure 2.6

    In the above figures, you can see whenever car user purchase a car they think about the pain of car, the comfort of the car, gearbox system like automatic or manual but car user never think like how will be the exhaust system will work, how the engine will generate torque, etc. so we can say this is some unessential thing for car user so car manufacturer will hide this unnecessary thing from car user and will just show the essential thing like paint, comfort, etc.

    Using abstract keywords we can hide unessential data from the user. We can use the Abstract Method and Abstract Class.

    pillars-of-c-sharp-encapsulation-image-3Figure 2.7

    The abstract class is restricted means no one can access the abstract class by making an object. if we want to use first we have to inherit that class from another class and by making an object of the child we can access abstract class.

    The abstract method we can use in only an abstract class. An abstract method hasn’t body. When we write an abstract method under the abstract class we are just writing the signature of that class. We can write its body in the inherited class.

    Let’s understand through coding example,

    pillars-of-c-sharp-encapsulation-code-3

    Figure 2.8

    In figure 2.8, you can see our animal class is abstract so it means no one can access that by making an object, or if he or she tries, it will generate an error like “cannot create an instance of an abstract
    class
    ”.

    pillars-of-c-sharp-encapsulation-code-4

    pillars-of-c-sharp-encapsulation-code-4-output

    Figure 2.9

    Now in figure 2.9, you can see to access the abstract class we inherit it from another class and it is a pig. so now we can access it and also you can see we have written the signature only of the abstract method under the abstract class. so the main body of that class is written in the inherited class that you can see. So at the end when we class both the regular method and the abstract method by making an object first it will print the abstract method and then it will print the regular method.

  4. Polymorphism
    Polymorphism was derived from two greek words Poly and Morph.Poly means Many and Morph means Forms. So we can say polymorphism means having many forms. It provides the ability for a class to have multiple implementations with the same name.Polymorphism has two types,

      1. Method OverLoading or Compile Time Polymorphism
      2. Method OverRiding or Run Time Polymorphism

        pillars-of-c-sharp-polymorphism

        Figure 3.0

        This is an example of method overloading where the method’s name is the same but has different parameters. Like in Figure 3.0, you can see TestData class have two methods and both methods name are the same Add but having different parameters like in the first method we are passing int a,b and c while in second method we are passing int a and b so this the method overloading so it will overload another method as per the passing parameters like if user gives 3 integer value it will overload method having two parameters and print answer.

        pillars-of-c-sharp-polymorphism-code

        Figure 3.1

        This is an example of method overriding so it means the method name will be the same, passing parameters will be also the same but the class will be different. like in this example you can see we have two methods with the same name and same parameter (gfg()) but having different classes. one is parent class and the second is child class. so this is an example of overriding.

Why?

Inheritance and Polymorphism = It is useful for code reusability: reuse fields and methods of an existing class when you create a new class.

Abstraction = To achieve security – hide certain details and only show the important details of an object.

Encapsulation = Used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them.

Conclusion

So in this blog, I have to try to explain all four pillars of OOPS in C# with as much as the simplest and realistic example so you can understand more. Now I think you can differentiate all four pillars with real-life examples as well as Coding examples.

Written By,

.NET Developer at Yudiz Solutions Pvt. Ltd