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,
- Abstraction
- Encapsulation
- Inheritance
- 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.
- Single Inheritance - contains one base class and one derived class
- Hierarchical Inheritance - Contains one base class and multiple derived classes of same base class
- Multilevel Inheritance - Contains a class derived from a derived class
- Multiple Inheritance - Contains several base class and a derived class
Single Inheritance
- using System;
-
- namespace Inheritance_Demo
- {
- public class Manager
- {
- public void Print()
- {
- Console.WriteLine("I am manager class");
- }
-
- }
-
- public class TeamLead : Manager
- {
- public new void Print()
- {
- Console.WriteLine("I am Team Lead class drived from manager class");
- }
- }
Hierarchical Inheritance
- using System;
-
- namespace Inheritance_Demo
- {
- public class Manager
- {
- public void Print()
- {
- Console.WriteLine("I am manager class");
- }
-
- }
-
- public class TeamLead : Manager
- {
- public new void Print()
- {
- Console.WriteLine("I am Team Lead class drived from manager class");
- }
- }
-
- public class Employee : Manager
- {
- public new void Print()
- {
- Console.WriteLine("I am employee class drived from manager class");
- }
- }
- }
Multilevel Inheritance
- using System;
-
- namespace Inheritance_Demo
- {
- public class Manager
- {
- public void Print()
- {
- Console.WriteLine("I am manager class");
- }
-
- }
-
- public class TeamLead : Manager
- {
- public new void Print()
- {
- Console.WriteLine("I am Team Lead class drived from manager class");
- }
- }
-
- public class Employee : TeamLead
- {
- public new void Print()
- {
- Console.WriteLine("I am employee class drived from manager class");
- }
- }
- }
Multiple Inheritance
Multiple inheritance is not allowed or not possible in C# class but It can be achieved through interface.
- using System;
-
- namespace Multiple_Inheritace_Demo
- {
- public class Doctor
- {
- public void doctor()
- {
- Console.WriteLine("I am doctor class");
- }
- }
- }
-
- using System;
-
- namespace Multiple_Inheritace_Demo
- {
- interface IAccountant
- {
- void accountant();
- }
- }
-
- using System;
-
- namespace Multiple_Inheritace_Demo
- {
- interface IEngineer
- {
- void engineer();
- }
- }
-
- using System;
-
- namespace Multiple_Inheritace_Demo
- {
- class Professional : Doctor, IAccountant,IEngineer
- {
- public void accountant()
- {
- Console.WriteLine("I am an accountant interface");
- }
-
- public void engineer()
- {
- Console.WriteLine("I am an engineer interface");
- }
- }
- }
-
- using System;
-
- namespace Multiple_Inheritace_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Professional professional = new Professional();
- professional.doctor();
- professional.accountant();
- professional.engineer();
- Console.ReadLine();
- }
- }
- }
Types of polymorphism
There are basically two types of polymorphism in C#.
- Static polymorphism (Compile Time polymorphism)
- 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.
- class MethodOverloading
- {
- public int Add(int firstNumber, int secondNumber)
- {
- return firstNumber + secondNumber;
- }
-
- public double Add(double firstNumber, double secondNumber, double thirdNumber)
- {
- return firstNumber + secondNumber +thirdNumber;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- MethodOverloading MO = new MethodOverloading();
- int Total= MO.Add(10, 20);
- double SecondTotal= MO.Add(10, 20, 30);
-
- Console.WriteLine("Total of first method:"+ Total);
- Console.WriteLine("Total of second method:"+ SecondTotal);
- }
- }
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.
- using System;
-
- namespace MethodOverriding_Demo
- {
- public class Customer
- {
- public string firstName;
- public string lastName;
-
- public virtual void CustomerFullName()
- {
- Console.WriteLine("Full Name:"+firstName+ " " +lastName);
- }
- }
-
- public class PlatinumCustomer : Customer
- {
- public override void CustomerFullName()
- {
- base.CustomerFullName();
- }
- }
-
- public class GoldCustomer : Customer
- {
- public override void CustomerFullName()
- {
- base.CustomerFullName();
- }
- }
-
- public class SilverCustomer : Customer
- {
- public override void CustomerFullName()
- {
- base.CustomerFullName();
- }
- }
- }
- using System;
-
- namespace MethodOverriding_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- PlatinumCustomer pc = new PlatinumCustomer();
- pc.firstName = "Farhan";
- pc.lastName = "Ahmed";
- pc.CustomerFullName();
- GoldCustomer gc = new GoldCustomer();
- gc.firstName = "Abdul";
- gc.lastName = "Jabbar";
- gc.CustomerFullName();
- SilverCustomer sc = new SilverCustomer();
- sc.firstName = "Abdul";
- sc.lastName = "Vaheed";
- sc.CustomerFullName();
- Console.ReadLine();
- }
- }
- }
What is class?
A class is the primary building block of object-oriented programming. A class is a blueprint.
- public class Students
- {
- public string firstName { get; set; }
- public string lastName { get; set; }
- }
What is object?
The object is an instance of a class.
- Students student = new Students();
- student.PrintFullName();
What are methods?
Methods are blocks of code that contain a series of statements.
- public void PrintFullName()
- {
- Console.WriteLine("Enter first name");
- firstName = Convert.ToString(Console.ReadLine());
-
- Console.WriteLine("Enter last name");
- lastName = Convert.ToString(Console.ReadLine());
-
- Console.WriteLine("Your FullName:" + firstName+" "+lastName);
- }