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 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 Concept 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?
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 redundant. Later if you want to change the logic, then you need to change at multiple 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 the Functional Programming Problem?
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 is understand OOPs Concepts in C#. The 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 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 these 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, 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 OOPs. OOPs provide some principles and by using those principles you 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 the real-world objects such as Person, Animal, Bike, Computer, 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 the 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 Customer, Manager, Clerk, Office Assistant, Marketing Executive, Computer, Printer, Chair, table, 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 are 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 Concept in C# are design patterns 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 change also should be accepted from external files like properties file, XML file, 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 some 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 C# article.