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 Concepts 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#.
- What are the Problems of Functional Programming?
- How we can overcome the Functional Programming Problem?
- What Is Object-Oriented Programming in C#?
- What are the OOPs Principles?
- Why do we need Object Oriented Programming in C#?
- Why do we need real-world objects in a Project?
- What types of programming languages come under the OOP system?
Object-Oriented Programming is a strategy or a style of developing applications or developing software. It is a methodology. Other methodologies are also there like Structured Programming, Procedural Programming, Modular Programming, or Aspect-Oriented programming. But nowadays, one of the well-known and famous styles is Object Orientation.
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 internal design but not external that is visible designing. 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 any product is developed, the same way a software product is developed by adopting object orientation.
If we talk a little bit about other engineering like suppose a civil engineer is constructing a building then first of all he/she will make a plan. They will make a design. While making a design, 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, will come up with some design that is the circuit design of that device on paper. Once it is finalized then he will start manufacturing the device.
So, on paper we make blueprints. We are not using paper nowadays. We have many computer editing tools. So let us just imagine on paper we are working on. In the same way for developing a software we have to do some paperwork, come up with a blueprint, and then start developing software. And a team of programmers can work over a single software and the work can be distributed easily and the work can be reused between the programmers very easily.
So, 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 also available like Procedural Programming, Modular Programming, Structured Programming, or Aspect Programming but Object-Oriented Programming is a well known and successful pattern 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.
You can open an account, you can deposit some amount, you can withdraw some amount, you can check your account balance or you can 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. 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 the functions and the user of your application will be utilizing these functions for performing his required task. So, you will develop a 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. These are 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 Electric bank.
What do we call these? you call them objects. So, the complete system for the government or a complete software for a government is a collection or set of objects. Now each object is having its relevant functions. So, a 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 them for larger and bigger tasks.
At that time, when C language or procedural language was there, everything was not computerized in the system. 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 on 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 then you can see all the things like a set of objects.
Back to the example, electric-related things are functions related to electricity, and transport-related functions are related to transport. 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 some goods. Let us see an automobile company that 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 a software only for payroll purpose or only for inventory purpose 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 a software for the entire organization then you have to see the things in the form of objects.
So, the inventory items are 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.
So that’s it. This is the introduction to object orientation in comparison with modular programming.
What are the Problems of Functional Programming?
Functional programming has the following problems.
In Functional 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.
It is not possible in functional 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.
As extensibility and reusability are not possible in functional programming, usually we end up with lots of functions and lots of scattered code.
As we don’t have Reusability, Extensibility, and Simplicity in functional Programming, so it is very difficult to manage and maintain the application code.
How we can overcome Functional Programming Problems?
We can overcome the functional programming problems (Reusability, Extensibility, Simplicity, and Maintainability) using Object-Oriented Programming. OOPs provide some principles and using those principles we can overcome the functional 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.
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.
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.
Because we don’t have extensibility and reusability in functional programming, we end up with lots of functions and lots of scattered code. In OOPs, this problem is addressed by using some concepts called Abstraction, Encapsulation, and Polymorphism.
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
Let’s understand the definitions of the OOPs Principle in this article. 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 call the object operations (i.e. methods) by hiding or removing its implementation details.
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 creating 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 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.
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). And finally, you need to give access to the Register method instead of the Validate, GetConnectionString, and SaveUser methods. This is what we discuss 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.
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 class and the class which inherits the members of the Parent class is called the Derived/ child class. 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
- Static polymorphism/compile-time polymorphism/Early binding
- 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, .Net, C++, etc.
Points to Remember:
- 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.
- 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.
- 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.
- 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.
- 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.