Object-Oriented Programming in C#
In this article, I am going to give you a basic understanding of Object-Oriented Programming 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 pointers in detail.
- Why do we need Object-Oriented Programming in C#?
- Why do we need real-world objects in a program?
- What Is Object-Oriented Programming in C#?
- How many OOPs principles do we have?
- What is Encapsulation?
- What is Abstraction?
- What is Inheritance?
- What is Polymorphism?
- What types of programming languages come under the OOP system?
Why do we need Object-Oriented Programming?
If we want to represent the real-world objects in a programming language for automating the business by achieving the security and reusability then we need the concept oops. OOPs provide some principles and by using those principles we can develop real-world objects in a programming language by achieving both security and reusability.
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 program?
We need real-world objects in a program 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.
So technically we call the above objects are business objects.
What Is Object-Oriented Programming?
OOPs (Object Oriented Programming System) is a concept (methodology) that provides a way of developing a program by creating partitioned memory area for both data and methods that can be used as a template for creating copies of such modules (objects) on demand.
Unlike procedural programming language, here in oops, programs are organized around objects and data rather than action and logic.
Points to Remember:
- Object-Oriented Programming principles are design patterns which 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 the 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 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.
- 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
How many OOPs principles do we have?
OOPs, provide 4 principles. They are
Let’s understand the definitions of OOPs Principle in this article. From the next article onwards we will discuss all these principles in details using some real-time examples.
What is Encapsulation?
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.
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 as Parent/base class and the class which inherits the members of the Parent class is called as Derived/ child class. We can achieve code reusability 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 is called Polymorphism. There are two types of polymorphism
Static polymorphism is achieved by using function overloading and operator overloading whereas dynamic polymorphism is achieved by using function overriding.
What is Abstraction?
The process of representing the essential features without including the background details is called as data 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.
Functions or methods are the good examples of abstraction because we never know what they do internally, what we know only is how to consume them.
What types of programming languages come under the OOP system?
The programming languages which implement all the above four principles provided by OOPs are called object-oriented programming languages. Examples: Java, .net, c++, etc.
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.