Object Oriented Programming System In C#

We have two various kinds of approaches for writing a program, which are shown below.
  1. Procedural oriented approach
  2. Object oriented approach
Procedural oriented approach

In procedural oriented approach, we have a set of procedures or functions to execute a piece of code or to perform some functionality. A programming language, which follows Procedural oriented approach is said to be procedural oriented programming language. In this approach, program is divided into various parts called a function/method. Procedural oriented approach follows top down approach. It doesn't contain any kind of access modifiers like public, private, internal etc..

As these are written in multiple function/method, we cannot perform the method hiding due to which we can't provide security for procedural oriented approach. It doesn't support function/method overloading.

Examples of procedural oriented approach are : C, VB , Pascal etc.

Object oriented approach 

In Object oriented approach, we have a collection of classes and an object.

A programming language, which follows object oriented approach is said to be object oriented programming language. In this approach, a program is divided into multiple parts called classes/objects. Object oriented approach follows bottom up approach, as we invoke after creating the object. It supports the access modifiers to a class and its members like variable or a method/property/constructor etc. It supports method due to which OOPS provides security in object oriented approach. It supports method overloading etc. The examples of object oriented approach are C++ , JAVA ,C# ,VB.NET etc. To achieve the above object oriented approach, we will the use classes and objects concept.

Class

Class is a collection of the data members and member functions.
(or) class is a collection of the variables and methods.
(or) class is a collection of the states and behaviors.

The data member/variable/state is used to represent some value.

Example

String EmployeeName="khaja moiz";

The member function/method/behavior is used to perform the functionality like for calculating an employee salary etc.

Example 

Calculating an employee salary, calculating employee hike etc.

Object

Object is nothing but an instance /physical representation of a class. To create the object, we will represent with new keyword. Object contains an instance variable and address/reference of the instance method. If the class is static class, we can access static class members, using the class name. If the class is an instance, we can access the instance class members by creating an object for the instance class.

Syntax for creating an object/accessing an instance class
  1. <classname> reference= new <classname>();  
  2. reference.Methodname();  
Syntax for accessing the static class
  1. <classname>.StaticMethodName();  
We have four different types of OOPS principles in C#, which are given below.
  1. Encapsulation
  2. Abstraction 
  3. Inheritance
  4. Polymorphism
Encapsulation

It is a container, which holds the data member and the member function.

Encapsulation in OOPS is wrapping of the states/variables/data members and behaviours/methods/member functions in a particular class.

(or)

It is a combination of methods and the variables or the properties in a single container is nothing but a class.

Example to understand Encapsulation
  1. Using system;  
  2. Namespace Encapsulation Example {  
  3.     class EmployeeClass {  
  4.         int EmployeeNo;  
  5.         string EmployeeName;  
  6.         Public void DisplayDetails() {  
  7.             Console.WriteLine(Employee Number is: "+ EmployeeNo);  
  8.                 Console.WriteLine(Employee Name is: "+ EmployeeName);  
  9.                 }  
  10.             }  
  11.         }  
Abstraction

Abstraction means showing the data, which is required and hiding the unwanted data or unnecessary data. We have two types of abstraction, which are given below.
  • Data Abstraction
  • Method Abstraction
Data Abstraction

It is a process of hiding the data, which is unwanted or unnecessary and showing only the relevant data to the user.

Example to understand Data Abstraction

  1. Using system;  
  2. namespace data abstraction example {  
  3.     class Employee {  
  4.         ulong EmployeeNo;  
  5.         string EmployeeName;  
  6.         double EmployeeSalary;  
  7.         String EmployeeDesignation;  
  8.         Internal Employee(ulong EmployeeNo, string EmployeeName, double EmployeeSalary, string EmployeeDesignation) {  
  9.             this.EmployeeNo = EmployeeNo;  
  10.             this.EmployeeName = EmployeeName;  
  11.             this.EmployeeSalary = Employeesalary;  
  12.             this.EmployeeDesignation = EmployeeDesignation;  
  13.         }  
  14.         Internal void DisplayEmployDetails() {  
  15.             Console.writeline("EmployeeNo is :" + EmployeeNo);  
  16.             console.writeline("EmployeeName is :" + EmployeeName);  
  17.             console.writeline("Employeesalary is :" + Employeesalary);  
  18.             console.writeline("Employeedesignation is :" + Employeedesignation);  
  19.         }  
  20.         internal void Hike() {  
  21.             // In this hike method we required employee details like EmployeeNo , EmployeeName and Employee Salary. EmployeeDesignation  
  22.             is an unwanted data  
  23.             for Hike Method.  
  24.         }  
  25.         class program {  
  26.             static void main() {  
  27.                 Employee emp = new Employee(1026, "khajaMoiz", 2000, "Software Developer");  
  28.                 emp.DisplayEmployDetails();  
  29.                 emp.Hike();  
  30.                 Console.Readline();  
  31.             }  
  32.         }  
  33.     }  
  34. }  
Method Abstraction

Method abstraction means invoking/accessing the particular method and hiding the other unwanted/unnecessary methods.

Example to understand Method Abstraction
  1. Using system;  
  2. namespace Method abstraction Example {  
  3.     class Employee {  
  4.         public void EmployeeNo(long EmployeeNo) {  
  5.             console.writeline("EmployeeNo is :" + EmployeeNo);  
  6.         }  
  7.         public void EmployeeName(string EmployeeName) {  
  8.             console.writeline("EmployeeName is :" + EmployeeName);  
  9.         }  
  10.         public void EmployeeSalary(double EmployeeSalary) {  
  11.             console.writeline("Employeesalary is :" + Employeesalary);  
  12.         }  
  13.         public void EmployeeDesignation(string EmployeeDesignation) {  
  14.             console.writeline("EmployeeDesignation is :" + EmployeeDesignation);  
  15.         }  
  16.     }  
  17.     class program {  
  18.         Employee emp = new Employee();  
  19.         emp.EmployeeName("khaja Moizuddin");  
  20.         emp.EmployeeSalary(2000);  
  21.         Console.Readline();  
  22.     }  
  23. }  
From the example given above, we are invoking the two methods, which are given below.
  1. emp.EmployeeName("khaja Moizuddin");  
  2. emp.EmployeeSalary(2000);  
It invokes EmployeeName and EmployeeSalary and hides the unwanted methods like EmployeeNo and EmployeeDesignation.

This is called Method Abstraction.

Inheritance

Inheritance means inheriting/deriving/accessing the members from one class to another class. If we want to implement inheritance, we should have more than one class.

A class from which we are inheriting is known as super class and a class, where we are receiving/accessing is called a sub class. Super class can also be called as a parent class or a base class. Sub class can also be called as a child class or a derived class. Using inheritance, we can access all the members from the super class/parent class/base class to the derived class/child class/sub class directly without creating the object or without using an objectname or a classname, we can access those members directly.

It supports re usability of the classes and its members.

We have five types of inheritance in C#, which are given below.
  • Single inheritance
  • Multi level inheritance
  • Multiple inheritance
  • Hybrid inheritance
  • Hierarchical inheritance.
Single inheritance

Inheriting/accessing the members from one class to another class is called single inheritance.

Syntax for Single inheritance
  1. class Employee {}  
  2. class Organisation: Employee {}  
In the example given above, we can access all the members of Employee class into Organisation class because of inheritance, as it allows us to access all the members from the parent class/base class to the child class/sub class without creating an object.

Example to understand Single inheritance
  1. using system;  
  2. namespace Single inheritance Example  
  3. class Employee {  
  4.     int employeeno;  
  5.     string employeename;  
  6.     double employeesalary;  
  7.     void EmployeeDetails() {  
  8.         console.writeLine("EmployeeNo is :" + EmployeeNo);  
  9.         console.writeLine("EmployeeName is :" + EmployeeName);  
  10.         console.writeLine("EmployeeSalary is :" + EmployeeSalary);  
  11.     }  
  12. }  
  13. class Organisation: Employee {  
  14.     string CompanyName;  
  15.     string CompanyAddress;  
  16.     int Pincode;  
  17.     void OrganisationDetails() {  
  18.         EmployeeDetails();  
  19.     }  
  20. }  
Here, in the example given above, an organisation class can access the method of an employee class because of inheritance.
While creating an object for the derived class, it automatically creates an object for the base class .

Multi Level inheritance

Inheriting /accessing the members of one super class to its derived class and again accessing the members of this derived class from other derived class is called multi level inheritance.

Syntax for multi level inheritance
  1. class Employee {}  
  2. class organisation: Employee {}  
  3. class Events: organisation {}  
In the example given above, we are accessing Employee super class in an organisation derived class and again access the organisation derived class from another derived class i.e. Events class, which is nothing but multi level inheritance.

Example to understand multi level inheritance
  1. using system;  
  2. namespace multi level inheritance {  
  3.     class Branch {  
  4.         int branchid;  
  5.         string branchname;  
  6.         string branchlocation;  
  7.         internal Branch(int branchid, string branchname, string branchlocation) {  
  8.             this.branchid = branchid;  
  9.             this.branchname = branchname;  
  10.             this.branchlocation = branchlocation;  
  11.         }  
  12.         internal void BranchDetails() {  
  13.             console.writeline("Branch id is :" + branchid);  
  14.             console.writeline("Branch name is :" + branchname);  
  15.             console.writeline("Branch location is :" + branchlocation);  
  16.         }  
  17.     }  
  18.     class Employee: Branch {  
  19.         int EmployeeNo;  
  20.         string EmployeeName;  
  21.         internal Employee(int EmployeeNo, string EmployeeName): Base(1077, "SBI""Hyderabad") {  
  22.             this.EmployeeNo = EmployeeNo;  
  23.             this.EmployeeName = EmployeeName;  
  24.         }  
  25.         internal void EmployeeDetails() {  
  26.             console.writeline("Employee Number is :" + EmployeeNo);  
  27.             console.writeline("Employee Name is:" + EmployeeName);  
  28.         }  
  29.     }  
  30.     class Salary: Employee {  
  31.         double basic;  
  32.         double hra;  
  33.         double da;  
  34.         double gross;  
  35.         internal Salary(double basic): base(1066, "khajaMoiz") {  
  36.             this.basic = basic;  
  37.             this.hra = hra;  
  38.             this.da = da;  
  39.             this.gross = gross;  
  40.         }  
  41.         internal void SalaryDetails() {  
  42.             hra = 0.4 * basic;  
  43.             da = 0.2 * basic;  
  44.             gross = hra + da + basic;  
  45.             console.writeline("Basic salary is:" + basic);  
  46.             console.writeline("hra is:" + hra);  
  47.             console.writeline("da is:" + da);  
  48.             console.writeline("gross is:" + gross);  
  49.         }  
  50.     }  
  51.     class program {  
  52.         static void main() {  
  53.             Salary objref = new Salary(12000);  
  54.             objref.SalaryDetails();  
  55.             Console.Readline();  
  56.         }  
  57.     }  
  58. }  
Multiple Inheritance

Inheriting or accessing multiple parent class/ multiple base class into a single derived class is called multiple inheritance.

C# doesn't support multiple inheritance by using classes, which will support only by using interfaces.

Syntax for Multiple Inheritance
  1. Interface IEmployee {}  
  2. Interface IOrganisation {}  
  3. class Events: IEmployee, IOrganisation {}  
Example to Understand Multiple Inheritance
  1. using system;  
  2. namespace Multiple Inheritance Example {  
  3.     interface IBasicMobileFeatures {  
  4.         void Calls();  
  5.         void alarmclock();  
  6.         void calculator();  
  7.     }  
  8.     class Iphone: IBasicMobileFeatures {  
  9.         public void Calls() {  
  10.             console.writeline("Iphone calls");  
  11.         }  
  12.         public void alarmclock() {  
  13.             console.writeline("Iphone alarmclock");  
  14.         }  
  15.         public void calculator() {  
  16.             console.writeline("Iphone calculator");  
  17.         }  
  18.     }  
  19.     class Blackberry: IBasicMobileFeatures {  
  20.         public void Calls() {  
  21.             console.writeline("Blackberry calls");  
  22.         }  
  23.         public void alarmclock() {  
  24.             console.writeline("Blackberry alarm clock");  
  25.         }  
  26.         public void calculator() {  
  27.             console.writeline("Blackberry calculator");  
  28.         }  
  29.     }  
  30.     interface ISmartphoneDevices {  
  31.         void WIFI();  
  32.         void Youtube();  
  33.         void Whatsapp();  
  34.     }  
  35.     class Samsung: IBasicMobileFeatures, ISmartphoneDevices // Implementing multiple inheritance.  
  36.     {  
  37.         public void calls() {  
  38.             console.writeline("Samsung calls");  
  39.         }  
  40.         public void alarmclock() {  
  41.             console.writeline("Samsung alarmclock");  
  42.         }  
  43.         public void calculator() {  
  44.             console.writeline("Samsung calculator");  
  45.         }  
  46.         public void WIFI() {  
  47.             console.writeline("Samsung's WIFI");  
  48.         }  
  49.         public void Youtube() {  
  50.             console.writeline("Samsung's youtube");  
  51.         }  
  52.         public void Whatsapp() {  
  53.             console.writeline("samsung's Whatsapp");  
  54.         }  
  55.     }  
  56.     class program {  
  57.         IBasicMobileFeatures objref = new Iphone();  
  58.         objref.calls();  
  59.         objref.alarmclock();  
  60.         objref.calculator();  
  61.         objref = new Blackberry();  
  62.         objref.calls();  
  63.         objref = alarmclock();  
  64.         objref.calculator();  
  65.         ISmartphoneDevices objref2 = (ISmartphoneDevices) objref1;  
  66.         objref2.WIFI();  
  67.         objref2.Youtube();  
  68.         objref2.Whatsapp();  
  69.         Console.Readline();  
  70.     }  
  71. }  
Hierarchical inheritance

Inheriting/accessing single super class into the multiple base classes/derived class is nothing but Hierarchical inheritance.

Syntax for Hierarchical inheritance
  1. class Employee {}  
  2. class Organisation: Employee {}  
  3. class Events: Employee {}  
Hybrid Inheritance

Hybrid Inheritance is a combination of any two inheritances.

Example 

A combination of Singe inheritance and a multi level inheritance
(or)
A combination of Multiple and Hierarchical inheritance etc.

Polymorphism

Implementing multiple methods with same name with different parameters is called Polymorphism.

There are two types of Polymorphism, which are given below.
  • Static Polymorphism or Function Overloading
  • Dynamic Polymorphism or Function Overriding.
Static Polymorphism or Function Overloading

Function Overloading means implementing multiple methods with the same name with different method parameters or method signature in a single class or a combination of base class and its derived class.

The method parameter or method signature depends on the components given below.
  • Number of parameters.
  • Type of parameters
  • Order of parameters.
Dynamic Polymorphism or Function Overriding

Function Overriding means implementing multiple methods with the same name and same method parameters or method signature in a combination of the base class and its derived class.

In parent class /base class, we will use a virtual keyword and in the derived class, we will use override keyword. To implement function overriding, we will create a super class reference variable and a sub class object. We cannot implement dynamic polymorphism in a single class.

Function Overriding depends on the things given below.
  • Method name should be same.
  • Signature/parameters should be same.
  • Return type should be same.
  • Access Modifier should be same.
Thanks & I hope this blog helps you.