Object Oriented Programming In C#

Introduction

In this article, I will explain object-oriented programming in C#. As we all know, C# is an object-oriented programming language. By using C# language we can develop different kinds of applications like a console application, window application, web application and mobile application etc. It is developed by Microsoft.

What is object-oriented programming?

Object-Oriented Programming is a technique to develop logical modules, such as classes, that contain properties, fields, and events. Object-Oriented Programming provides many concepts such as abstraction, encapsulation, inheritance, and polymorphism.

There are four main pillars of object-oriented programming,

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

Abstraction

Abstraction is a process of hiding irrelevant information and showing only relevant information to the user.

Encapsulation

Encapsulation is an ability to hide data and behavior that are not necessary to its use by using access modifier. Encapsulation is defined as "the process of enclosing one or more items within a physical or logical package".

Inheritance

Inheritance describes the ability to create new classes based on an existing class.

Polymorphism

Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.

Types of inheritance

There are four types of Inheritance in Object-oriented programming.

  1. Single Inheritance - contains one base class and one derived class
  2. Hierarchical Inheritance - Contains one base class and multiple derived classes of same base class
  3. Multilevel Inheritance - Contains a class derived from a derived class
  4. Multiple Inheritance - Contains several base class and a derived class

Single Inheritance

  1. using System;  
  2.   
  3. namespace Inheritance_Demo  
  4. {  
  5.     public class Manager  
  6.     {  
  7.         public void Print()  
  8.         {  
  9.             Console.WriteLine("I am manager class");  
  10.         }  
  11.   
  12.     }  
  13.   
  14.     public class TeamLead : Manager  
  15.     {  
  16.         public new void Print()  
  17.         {  
  18.             Console.WriteLine("I am Team Lead class drived from manager class");  
  19.         }  
  20.     }  

Hierarchical Inheritance

  1. using System;  
  2.   
  3. namespace Inheritance_Demo  
  4. {  
  5.     public class Manager  
  6.     {  
  7.         public void Print()  
  8.         {  
  9.             Console.WriteLine("I am manager class");  
  10.         }  
  11.   
  12.     }  
  13.   
  14.     public class TeamLead : Manager  
  15.     {  
  16.         public new void Print()  
  17.         {  
  18.             Console.WriteLine("I am Team Lead class drived from manager class");  
  19.         }  
  20.     }  
  21.   
  22.     public class Employee : Manager  
  23.     {  
  24.         public new void Print()  
  25.         {  
  26.             Console.WriteLine("I am employee class drived from manager class");  
  27.         }  
  28.     }  
  29. }  

Multilevel Inheritance

  1. using System;  
  2.   
  3. namespace Inheritance_Demo  
  4. {  
  5.     public class Manager  
  6.     {  
  7.         public void Print()  
  8.         {  
  9.             Console.WriteLine("I am manager class");  
  10.         }  
  11.   
  12.     }  
  13.   
  14.     public class TeamLead : Manager  
  15.     {  
  16.         public new void Print()  
  17.         {  
  18.             Console.WriteLine("I am Team Lead class drived from manager class");  
  19.         }  
  20.     }  
  21.   
  22.     public class Employee : TeamLead  
  23.     {  
  24.         public new void Print()  
  25.         {  
  26.             Console.WriteLine("I am employee class drived from manager class");  
  27.         }  
  28.     }  
  29. }  

Multiple Inheritance

Multiple inheritance is not allowed or not possible in C# class but It can be achieved through interface.

  1. using System;  
  2.   
  3. namespace Multiple_Inheritace_Demo  
  4. {  
  5.     public class Doctor  
  6.     {  
  7.         public void doctor()  
  8.         {  
  9.             Console.WriteLine("I am doctor class");  
  10.         }  
  11.     }  
  12. }  
  13.   
  14. using System;  
  15.   
  16. namespace Multiple_Inheritace_Demo  
  17. {  
  18.     interface IAccountant  
  19.     {  
  20.         void accountant();  
  21.     }  
  22. }  
  23.   
  24. using System;  
  25.   
  26. namespace Multiple_Inheritace_Demo  
  27. {  
  28.     interface IEngineer  
  29.     {  
  30.         void engineer();  
  31.     }  
  32. }  
  33.   
  34. using System;  
  35.   
  36. namespace Multiple_Inheritace_Demo  
  37. {  
  38.     class Professional : Doctor, IAccountant,IEngineer  
  39.     {  
  40.         public void accountant()  
  41.         {  
  42.             Console.WriteLine("I am an accountant interface");  
  43.         }  
  44.   
  45.         public void engineer()  
  46.         {  
  47.             Console.WriteLine("I am an engineer interface");  
  48.         }  
  49.     }  
  50. }  
  51.   
  52. using System;  
  53.   
  54. namespace Multiple_Inheritace_Demo  
  55. {  
  56.     class Program  
  57.     {  
  58.         static void Main(string[] args)  
  59.         {  
  60.             Professional professional = new Professional();  
  61.             professional.doctor();  
  62.             professional.accountant();  
  63.             professional.engineer();  
  64.             Console.ReadLine();  
  65.         }  
  66.     }  
  67. }  

Types of polymorphism

There are basically two types of polymorphism in C#.

  1. Static polymorphism (Compile Time polymorphism)
  2. Dynamic polymorphism (Run Time polymorphism)

Static Polymorphism

Static polymorphism is also called Compile Time polymorphism. In Static polymorphism, methods are overloaded with the same name but have different signatures. So, it is called method overloading.

  1. class MethodOverloading  
  2.     {  
  3.         public int Add(int firstNumber, int secondNumber)  
  4.         {  
  5.             return firstNumber + secondNumber;  
  6.         }  
  7.   
  8.         public double Add(double firstNumber, double secondNumber, double thirdNumber)  
  9.         {  
  10.             return firstNumber + secondNumber +thirdNumber;  
  11.         }  
  12.     }  
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             MethodOverloading MO = new MethodOverloading();  
  6.             int Total= MO.Add(10, 20);  
  7.             double SecondTotal= MO.Add(10, 20, 30);  
  8.   
  9.             Console.WriteLine("Total of first method:"+ Total);  
  10.             Console.WriteLine("Total of second method:"+ SecondTotal);  
  11.         }  
  12.     }  

Dynamic Polymorphism

Dynamic polymorphism is also called Run Time polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.

  1. using System;  
  2.   
  3. namespace MethodOverriding_Demo  
  4. {  
  5.     public class Customer  
  6.     {  
  7.         public string firstName;  
  8.         public string lastName;  
  9.   
  10.         public virtual void CustomerFullName()  
  11.         {  
  12.             Console.WriteLine("Full Name:"+firstName+ " " +lastName);  
  13.         }  
  14.     }  
  15.   
  16.     public class PlatinumCustomer : Customer  
  17.     {  
  18.         public override void CustomerFullName()  
  19.         {  
  20.             base.CustomerFullName();  
  21.         }  
  22.     }  
  23.   
  24.     public class GoldCustomer : Customer  
  25.     {  
  26.         public override void CustomerFullName()  
  27.         {  
  28.             base.CustomerFullName();  
  29.         }  
  30.     }  
  31.   
  32.     public class SilverCustomer : Customer  
  33.     {  
  34.         public override void CustomerFullName()  
  35.         {  
  36.             base.CustomerFullName();  
  37.         }  
  38.     }  
  39. }  
  1. using System;  
  2.   
  3. namespace MethodOverriding_Demo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             PlatinumCustomer pc = new PlatinumCustomer();  
  10.             pc.firstName = "Farhan";  
  11.             pc.lastName = "Ahmed";  
  12.             pc.CustomerFullName();  
  13.             GoldCustomer gc = new GoldCustomer();  
  14.             gc.firstName = "Abdul";  
  15.             gc.lastName = "Jabbar";  
  16.             gc.CustomerFullName();  
  17.             SilverCustomer sc = new SilverCustomer();  
  18.             sc.firstName = "Abdul";  
  19.             sc.lastName = "Vaheed";  
  20.             sc.CustomerFullName();  
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
  24. }  

What is class?

A class is the primary building block of object-oriented programming. A class is a blueprint.

  1. public class Students  
  2. {  
  3.     public string firstName { get; set; }  
  4.     public string lastName { get; set; }  
  5. }  

What is object?

The object is an instance of a class. 

  1. Students student = new Students();  
  2. student.PrintFullName();  

What are methods?

Methods are blocks of code that contain a series of statements.

  1. public void PrintFullName()  
  2. {  
  3.     Console.WriteLine("Enter first name");  
  4.     firstName = Convert.ToString(Console.ReadLine());  
  5.   
  6.     Console.WriteLine("Enter last name");  
  7.     lastName = Convert.ToString(Console.ReadLine());  
  8.   
  9.     Console.WriteLine("Your FullName:" + firstName+" "+lastName);  
  10. }