Object Oriented Programming in C#
(Object-Oriented Programming) OOP concept is a must to be a good programmer. This is the important design principle, In coders’ life. OOP is all about how to write a code in an organized code structure. This design principle gives you a good knowledge of coding skills. Rather than going through technical terms, I am going to help you to understand this concept through real-life examples.
- Class
- Object
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation
Class
Class is a blueprint of an object. This is a mandatory concept in OOP. If you consider the real world, a class is like our school bag. We filled it with books, water bottle, geometry box, pencil box, and so on. Class is a collection of objects. Class does not contain specific memory allocation.
Abstraction
Abstraction is a class that is used to hide the basic functionality of the user. Let us consider our image sharing process through Bluetooth. What we need is to share our desired images with our friend’s phone. Here we do not care how to share those images through Bluetooth. That information is hidden from the user. We used the abstract keyword to create the Abstract class. We cannot create instances from the Abstract class as we create an instance to Class. This is the main difference between Class and Abstract Class.
So, like this way we create an Abstract class in c#. As I said we cannot create an instance for Abstract class. Therefore, instead of creating an instance, we used to inherit the Abstract class to another class.
Consider the Console.WriteLine() in C#. It contains lots of methods and operations. But, we are not aware of those functions. We just use that method to print the data. In this way, Abstraction is hiding critical and irrelevant information and provides relevant information to the user. We just focus on the performance of the object instead of how it does the task. This is how Abstraction is used in OOP. Abstraction used to solve the problem at the design level. It provides an outer layout of the design.
When a class uses another class’s property like object, methods, it is simply defined as Inheritance. Let us consider our family; we are transferring some genetic characteristics throughout ancestry like skin color, hair type, face features and etc. In that way, the child class uses all the properties from the parent class. So we group inheritance concepts as Base class (parent class) and Derived class (child class).
Polymorphism
- Static Polymorphism (Compile time polymorphism)
- Dynamic Polymorphism (Runtime polymorphism)
01. Static or Compile Time Polymorphism
This is a concept of method overloading. If a class has methods or functions which are having the same name but different signatures (parameters), called method overloading. During the compile time, it decides which method or function should be called. Therefore, it is referred to as compile-time polymorphism.
02. Dynamic or Runtime Polymorphism
This is the concept of method overriding. Here method’s name or function’s name and its parameters (signatures) are the same but it contains different implementations. Through this we are going to use the same concept of Inheritance, but in a different way.
It has base and derived classes. Derived class has the same name and same parameter as base class. While compiling time, the compiler is not aware of the implementation. It’s just check the method’s name. Therefore, compiler never throws an error message. During the runtime only the compiler will decide which method should be called. If that method does not exist, then it will throw an error message. Due to this functionality, it is called runtime polymorphism.Encapsulation.
This is the next concept of OOP. It is wrapping the data and methods as a single unit. We can simply say that it is hiding the internal details of an object.
Let's consider our DVD player. It is encapsulated with a cover. We never operate the DVD by opening the back cover. We just operate through the remote controller. So like this way encapsulation is working on the OOP concept. Technically, encapsulation is a trick to protect the information from one object to another object.
Comments