Object Oriented Programming in C#

Object Oriented Programming (OOPs) in C#

In this article, I am going to give an overview of Object-Oriented Programming in C#. Object-Oriented Programming commonly known as OOPs is a techniquenot 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 pointers in detail.

  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?
What are the Problems of Functional Programming?

Functional programming has the following problems.

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

What are the Problems of Functional Programming?

Reusability:

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.

Extensibility:

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.

Simplicity:

As extensibility and reusability are not possible in functional 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 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?

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.

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 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 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.

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?

OOPs, provide 4 principles. They are

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

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 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 to this method as shown below.

Object-Oriented Programming in C#

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 Parent/base class and the class which inherits the members of the Parent class is called 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

  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 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 as 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 Bank object, we must also have to create all the above objects because without all the above objects we cannot run 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:
  1. Object-Oriented Programming principles 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.
  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 change also should be accepted from external files like properties file, XML file, etc. Scalability is achieved by developing classes by integrating them in the 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 customer 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 some examples. Here, in this article, I try to explain the basics of Object-Oriented Programming in C#. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *