Object Oriented Programming (OOPs) in C#

Object-Oriented Programming (OOPs) in C# | OOPs Concept in C#

In this article, I am going to give an overview of Object-Oriented Programming (OOPs) in C# i.e. going to discuss the OOPs Principles in C#. Object-Oriented Programming commonly known as OOPs is a technique, not a technology. It means it doesn’t provide any syntaxes or APIs instead it provides suggestions to design and develop objects in programming languages. As part of this article, we are going to cover the following OOPs concepts in C#.

  1. What are the Problems of Functional Programming?
  2. How we can overcome the Functional Programming Problem?
  3. What Is Object-Oriented Programming in C#?
  4. What are the OOPs Principles?
  5. Why do we need Object Oriented Programming in C#?
  6. Why do we need real-world objects in a Project?
  7. What types of programming languages come under the OOP system?
How do we Develop Applications?

Object-Oriented Programming is a strategy that provides some principles for developing applications or developing software. It is a methodology. Like OOPs, other methodologies are also existing such as Structured Programming, Procedural Programming, or Modular Programming. But nowadays, one of the well-known and famous styles is Object Orientation i.e. Object-Oriented Programming.

Nowadays, all most all the latest programming languages support object orientation and this object orientation is more related to the designing of software and this deals with the internal design of the software, not the external design. So, it is nowhere related to the users of the software, it is related to the programmers who are working on developing software.

With the help of Object Orientation, the application development or the programming becomes more and more systematic and we can follow the procedures of engineering to develop software. Like in other engineering how a product is developed, in the same way, a software product is developed by adopting object orientation.

If we talk a little bit about other engineering like a civil engineer is constructing a building then first of all he/she will make a plan or will make a design.  While making a design or plan, they may be having many options but they will select one of the designs and finalize it. Then once it is finalized as a blueprint on paper, then they will start constructing. In the same way, an electronic engineer, when he is manufacturing any device, he will come up with some design that is the circuit design of that device on paper. And once that design or blueprint is finalized then he will start manufacturing the device.

So, the point that you need to keep the focus on is on paper we make blueprints or designs or plans. Nowadays, we are not using paper. We have many computer editing tools and we can use those tools to make out designs. But let us just imagine on paper we are working on and we are preparing our design or blueprint or plan on paper. In the same way for developing software we have to do some paperwork, we have to come up with a blueprint or design or plan, and then we need to start developing the application. And a team of programmers can work on a single project and the work can be distributed easily and the work can be reused between the programmers very easily.

So, the Object Orientation all depends on how you see the system or how you understand the system. Looking at the system and your perspective is more important. So, if you understand the system perfectly and if your perspective is very clear then you can develop a better system.

Not just Object-Oriented Programming, there are other programming styles that are also available like Procedural Programming, Modular Programming, or Structured Programming but Object-Oriented Programming is a well-known and successful one for software development, and most of the latest languages that are used for application development, they are object-oriented.

Object-Oriented vs Modular Programming

Now I will explain to you Object Orientation by comparing it with Modular Programming. The reason is that people who came to learn C#, already know the C language. The C programming language supports Modular or Procedural Programming. So based on that I can give you the idea of how object orientation is different from modular programming. Let us compare Object-Oriented vs Modular Programming through some examples.

So first we are taking an example of a bank. If you’re developing an application for a bank using modular programming then how do you see the system, how do you see the working of a bank, and what will be your design? That basically depends on how you understand it and how you see the system. So let us see how we are looking at the bank system using modular programming.

In a bank, you can open an account, you can deposit an amount, you can withdraw an amount, you can check your account balance or you can also apply for a loan, and so on. So, these are the things that you can do at the bank.

So ‘Open Account’, ‘Deposit’, ‘Withdraw’, ‘Check Balance’ and ‘Apply Loan’ are functions. All these are nothing but functions. And you can do the specific operations by calling that specific function.  So, if you’re developing software for a bank, it is nothing but a collection of functions. So, the bank application will be based on these functions and the user of the bank application will be utilizing these functions for performing his required task. So, you will develop software in the form of a set of functions in Modular Programming.

Now for object orientation, we would take some different examples. The government provides a lot of services like electric service, water supply service, education, transport, and even government can have banks and so on. So, these are the different departments of a government. Now, what you can do in this department as a user? You can apply for a new connection, or you can close your connection if you want if you have extra connections, or you can make a bill payment, then what these are? These are functions belonging to the Electric Department.

Now in the same way bank is also there, the same functions like account open, deposit, withdraw, check balance, apply for a loan, etc. are also there. These are functions belonging to Bank.

What do we call these? We call them objects. So, the complete system for the government or a complete software for a government is a collection of objects. Now each object is having its relevant functions. So, complete software is a collection of objects and objects containing functions as well as data related to those functions.

And in Modular Programming, the system was a collection of functions. So, if you compare them now, in modular programming we are looking at the very close level and in object-oriented programming, we are looking at a little far away level.

So, if you look at things closer like if you go to the bank then everything inside that is nothing but a set of functions. When initially computer science was introduced, computers were used for performing some simple or very small tasks. Then later on people started using computers for larger and bigger tasks.

At that time, when C language or procedural language was introduced, everything was not computerized. For any organization, at that time, everything was not computerized, rather they computerized some part of it, like the account part of the system or payroll part of the business. So, when the programmers were working in any particular department then they were looking at everything as functions. But now we are developing software at a larger scale for a big organization as a complete one single software and there you can see all the things like a set of objects.

Back to the example, electric-related things are functions related to the electricity department, and transport-related functions are related to the transport department. So, we have increased our level of looking at the system because earlier we were solving small problems but now, we are solving bigger problems.

Then there was a need for some methodology that should support software development easily. So yes, object orientation is best suitable for large-size systems. 

Why Object Orientation?

Let us talk about a manufacturing firm which manufactures cars or vehicles. If you look at that manufacturing farm then it may be working in the form of departments like one is an inventory department that maintains the stock of raw material and one is manufacturing that is the production work that they do and one department will be looking at sales and one department is looking at marketing and one is about payroll and one is for accounts and so on. So, there may be many departments.

Now if you are developing software only for payroll purposes or only for inventory purposes then you may look at the system just like a modular approach and in that you can find the functions like placing an order and checking the item in stock. These types of things you can have a set of functions in that, so you can develop the software only for the inventory system as a collection of functions but when you are developing software for the entire organization then you have to see the things in the form of objects.

So, the inventory item is an object, an employee is an object, an account is an object, and a product manufacturer is an object, the machines that are used for production are an object. So, all these things are objects. Here, you need to see the things in the form of objects and define their data and the functions that they’re performing. We are looking at the system at a higher level. So, we can adopt object orientation.

What are the Problems of Modular Programming?

Modular programming has the following problems.

  1. Reusability
  2. Extensibility
  3. Simplicity
  4. Maintainability

What are the Problems of Functional Programming?

Reusability:  In Modular Programming, we need to write the same code or logic at multiple places which increases the code duplication. Later if we want to change the logic, then we need to change it at all places.

Extensibility: It is not possible in modular programming to extend the features of a function. Suppose you have a function and you want to extend it with some additional features then it is not possible. You have to create a completely new function and then change the function as per your requirement.

Simplicity: As extensibility and reusability are not possible in Modular Programming, usually we end up with lots of functions and lots of scattered code.

Maintainability: As we don’t have Reusability, Extensibility, and Simplicity in modular Programming, it is very difficult to manage and maintain the application code.

How we can overcome Modular Programming Problems?

We can overcome the modular programming problems (Reusability, Extensibility, Simplicity, and Maintainability) using Object-Oriented Programming. OOPs provide some principles and using those principles we can overcome Modular Programming Problems.

What Is Object-Oriented Programming?

Let us understand Object-Oriented Programming i.e. OOPs Concepts using C#. Object-Oriented Programming (OOPs) in C# is a design approach where we think in terms of real-world objects rather than functions or methods. Unlike procedural programming language, here in oops, programs are organized around objects and data rather than action and logic. Please have a look at the following diagram to understand this better.

What Is Object-Oriented Programming?

Reusability:

To address reusability, object-oriented programming provides something called Classes and Objects. So, rather than copy-pasting the same code again and again in different places what you can do here is, create a class and make an instance of the class which is called an object, and reuses them whenever you want.

Extensibility:

Suppose you have a function and you want to extend it with some new features that were not possible with functional programming. You have to create a completely new function and then change the whole function whatever you want. In OOPs, this problem is addressed by using some concepts called Inheritance, Aggregation, and Composition. In our upcoming article, we will discuss all these concepts in detail.

Simplicity:

Because we don’t have extensibility and reusability in modular programming, we end up with lots of functions and lots of scattered code and from anywhere we can access the functions, security is less. In OOPs, this problem is addressed by using some concepts called Abstraction, Encapsulation, and Polymorphism.

Maintainability:

As OOPs address Reusability, Extensibility, and Simplicity, we have good maintainable code and clean code which increases the maintainability of the application.

What are the OOPs Principles or OOPs Concepts in C#?

OOPs, provide 4 principles. They are

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

Note: Don’t consider Class and Objects as OOPs principle. We use class and objects to implement OOPs Principles.

Let’s understand the definitions of the OOPs Principle in this session. From the next article onwards, we will discuss all these principles in detail using some real-time examples.

What are Abstraction and Encapsulation?

The process of representing the essential features without including the background details is called Abstraction. In simple words, we can say that it is a process of defining a class by providing necessary details to the external world which are required by hiding or removing unnecessary things.

The process of binding the data and functions together into a single unit (i.e. class) is called Encapsulation. In simple words, we can say that it is a process of defining a class by hiding its internal data members from outside the class and accessing those internal data members only through publicly exposed methods or properties. Data encapsulation is also called data hiding because by using this principle we can hide the internal data from outside the class.

Abstraction and Encapsulation are related to each other. We can say that Abstraction is logical thinking whereas Encapsulation is its physical implementation.

Understanding Abstraction and Encapsulation with an Example:

Let us understand Abstraction and Encapsulation Principles with an example. Suppose you want to design one class for providing the register functionality of a user. For that what you need to do is, first you need to get the data and validate the data, then you need to get the connection string for the database, and finally, you need to save the data in the database. And for this, you have three methods i.e. Validate, GetConnectionString, and SaveUser. If you provide access to these three methods to the users of this class, then he may end up calling these methods in the wrong order or it may be possible that he may forget to call any of these methods.

So, here, what you need to do is, you need to create one method called Register and as part of that method you need to call all these methods (Validate, GetConnectionString, and SaveUser) in a proper sequence. And finally, you need to give access to the Register method instead of the Validate, GetConnectionString, and SaveUser methods. This is what we discussed is nothing but abstraction. How you implement this is nothing but encapsulation. So, here, you need to create the Validate, GetConnectionString, and SaveUser methods with private access specifier so that the user cannot access these methods. And make the Register method as Public so that the user can access this method as shown below.

OOPs Concepts in C#

We can achieve code Simplicity through Encapsulation and Abstraction.

What is Inheritance?

The process by which the members of one class are transferred to another class is called inheritance. The class from which the members are transferred is called the Parent/Base/Superclass and the class which inherits the members of the Parent/Base/Superclass is called the Derived/Child/Subclass. We can achieve code extensibility through inheritance.

What is Polymorphism?

The word Polymorphism is derived from the Greek word, where Poly means many and morph means faces/ behaviors. So, the word polymorphism means the ability to take more than one form. Technically, we can say that when the same function/operator will show different behaviors by taking different types of values or with a different number of values called Polymorphism. There are two types of polymorphism

  1. Static polymorphism/compile-time polymorphism/Early binding
  2. Dynamic polymorphism/Run time polymorphism/Late binding

Static polymorphism is achieved by using function overloading and operator overloading whereas dynamic polymorphism is achieved by using function overriding.

Why do we need Object-Oriented Programming (OOPs) in C#?

If you want to represent the real-world objects in a programming language for automating the business by achieving Reusability, Extensibility, Simplicity, and Maintainability, then you need the concept of OOPs. OOPs provide some principles and by using those principles we can develop real-world objects in a programming language by achieving Reusability, Extensibility, Simplicity, and Maintainability.

All living and non-living things are considered objects. So real-world objects such as Persons, Animals, Bikes, Computers, etc. can be developed in object-oriented programming languages by using the OOPs concept.

Why do we need real-world objects in a Project?

We need real-world objects in a project because real-world objects are part of our business. As we are developing applications (software) for automating the business, we must have to create the business-related real-world objects in the project.

For example, to automate the Bank business we must create real-world objects like Customers, Managers, Clerks, Office Assistants, Marketing Executives, Computers, Printers, Chairs, tables, etc. So along with the Bank object, we must also have to create all the above objects because without all the above objects we cannot run a Bank business. Technically we call the above objects business objects.

What types of programming languages come under the OOP system?

The programming languages which implement all the four principles provided by OOPs are called object-oriented programming languages. Examples: Java, C#, C++, etc.

Points to Remember:
  1. Object-Oriented Programming Principles or OOPs Concepts in C# are design principles that suggest how we should develop a program so that we can reuse it from other layers of the project effectively and with high scalability.
  2. Scalability means we have to develop the project in such a way that it should accept future changes without doing major changes in the project, that small changes also should be accepted from external files like properties files, XML files, etc. Scalability is achieved by developing classes by integrating them in a loosely coupled way.
  3. We should develop the project with scalability as there will be a growth in business, according to the growth in business we must add required changes to the project with minimal modifications.
  4. As a developer, we must remember that in the initial stage of business customer never makes a significant investment. As the business grows customers increase investment according to the growing new requirements are added to the projects. To add those new requirements we should not design the project entirely.
  5. So we must design the project by following OOPs principles strictly even though they are not needed at the initial stage but for accepting future changes

In the next article, I am going to discuss the Class and Objects in C# with Examples. Here, in this article, I try to explain the basics of Object-Oriented Programming (OOPs) in C#. I hope you enjoy OOPs Concept in the C# article.

2 thoughts on “Object Oriented Programming (OOPs) in C#”

  1. blank

    **Data encapsulation is also called data hiding because by using this principle we can hide the internal data from outside the class.**

    Data encapsulation and data hiding both are different

Leave a Reply

Your email address will not be published.