We have two various kinds of approaches for writing a program, which are shown below.
- Procedural oriented approach
- 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 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
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
- <classname> reference= new <classname>();
- reference.Methodname();
Syntax for accessing the static class
- <classname>.StaticMethodName();
We have four different types of OOPS principles in C#, which are given below.
- Encapsulation
- Abstraction
- Inheritance
- 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
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
- Using system;
- Namespace Encapsulation Example {
- class EmployeeClass {
- int EmployeeNo;
- string EmployeeName;
- Public void DisplayDetails() {
- Console.WriteLine(Employee Number is: "+ EmployeeNo);
- Console.WriteLine(Employee Name is: "+ EmployeeName);
- }
- }
- }
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.
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
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
- Using system;
- namespace data abstraction example {
- class Employee {
- ulong EmployeeNo;
- string EmployeeName;
- double EmployeeSalary;
- String EmployeeDesignation;
- Internal Employee(ulong EmployeeNo, string EmployeeName, double EmployeeSalary, string EmployeeDesignation) {
- this.EmployeeNo = EmployeeNo;
- this.EmployeeName = EmployeeName;
- this.EmployeeSalary = Employeesalary;
- this.EmployeeDesignation = EmployeeDesignation;
- }
- Internal void DisplayEmployDetails() {
- Console.writeline("EmployeeNo is :" + EmployeeNo);
- console.writeline("EmployeeName is :" + EmployeeName);
- console.writeline("Employeesalary is :" + Employeesalary);
- console.writeline("Employeedesignation is :" + Employeedesignation);
- }
- internal void Hike() {
- // In this hike method we required employee details like EmployeeNo , EmployeeName and Employee Salary. EmployeeDesignation
- is an unwanted data
- for Hike Method.
- }
- class program {
- static void main() {
- Employee emp = new Employee(1026, "khajaMoiz", 2000, "Software Developer");
- emp.DisplayEmployDetails();
- emp.Hike();
- Console.Readline();
- }
- }
- }
- }
Method Abstraction
Method abstraction means invoking/accessing the particular method and hiding the other unwanted/unnecessary methods.
Example to understand Method Abstraction
Method abstraction means invoking/accessing the particular method and hiding the other unwanted/unnecessary methods.
Example to understand Method Abstraction
- Using system;
- namespace Method abstraction Example {
- class Employee {
- public void EmployeeNo(long EmployeeNo) {
- console.writeline("EmployeeNo is :" + EmployeeNo);
- }
- public void EmployeeName(string EmployeeName) {
- console.writeline("EmployeeName is :" + EmployeeName);
- }
- public void EmployeeSalary(double EmployeeSalary) {
- console.writeline("Employeesalary is :" + Employeesalary);
- }
- public void EmployeeDesignation(string EmployeeDesignation) {
- console.writeline("EmployeeDesignation is :" + EmployeeDesignation);
- }
- }
- class program {
- Employee emp = new Employee();
- emp.EmployeeName("khaja Moizuddin");
- emp.EmployeeSalary(2000);
- Console.Readline();
- }
- }
From the example given above, we are invoking the two methods, which are given below.
- emp.EmployeeName("khaja Moizuddin");
- 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
Inheriting/accessing the members from one class to another class is called single inheritance.
Syntax for Single inheritance
- class Employee {}
- 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
Example to understand Single inheritance

Join the conversation! Your thoughts help the community grow.