Introduction
To start programming more freshly or to clear some interview even though you’re experienced, you must go through this concept. It is of the fundamental concepts one should know to become a successful software developer. To clear this concept, please go through the following article.
OOPs are concepts that deal with real-world scenarios, such as classes and objects, encapsulation and abstraction, etc.
Before going into detail, read about heap and stack memory management.
Heap memory is for dynamic memory allocation eg reference type like classes, string.
Stack memory is for static memory allocation Value type like int, boolean, struct, etc.
Classes and Objects
Classes we can simply define as Entity, which is a distinct and independent existence. For example:
Car refers to a class that can have various attributes, like model, year, colour, etc and methods that we require to get speed(), so we call this a function. To call this functionality or to assign these attributes, we have to put the class in memory, which we can do by reference to the class by creating the object of the class.
Eg Car Hondacity= new Car()
Here, hondacity is an object for the car class which contains its own colour, brand, speed, etc.
So in a nutshell, a class is a user defined blueprint or prototype for which the object is created. Every object that we create has its own property and method.
A class can be declared using following access specifier which limits the accessibility of classes to other classes.
-
Public
-
Private
-
Protected
-
Internal
-
Protected internal
Public class
Public class Classname { ....}
If we define a class as public, then it’s accessed from anywhere. This type of class can be accessible from the same assembly, as well from another assembly which references it.
Private class
Private class Classname {.....}
The scope of accessibility is within the same class. Outside the class, they are not accessible.
Protected class
Protected class Classname {...}
Accessibility is within the class as well as the class which is derived from it.
Internal class
Internal class Classname{...}
These classes are accessible within the same assembly and not from outside the assembly (dll)
Protected Internal
Protected internal Classname{...}
The same assembly as well as outside the assembly, but the class is derived from this assembly.
Abstraction and encapsulation
Abstraction
This is a property by which essential details are shown to the user and non-essential details are not shown to the user, rather it it is hidden from the user We can achieve abstraction by implementing an abstract class.
E.g., in the real world when a user logs in to any domain, the user only needs to know the username and password and when it's correct, it will successfully login. Here, the user is unaware of the complex logic behind this interface. Here, abstraction comes into the picture to display only the details where he/she knows how to log in, without knowing the inner details of how the functionality works.
Encapsulation
Hiding the data means providing only relevant data to the user by using access a specifier such as public, protected, and private. The major difference between abstraction and encapsulation is that abstraction hides the inner details where encapsulation hides the data details from the outside world.
Inheritance
This means “a thing that is inherited”. In C#, we can apply inheritance in a class by inheriting the member of another class called the derived class, and the class whose property is inherited is called the base class.
For example, if we have a class name employee, which has attributes like name, age, and department, and suppose if we have to create another class like Class for Hr department class...
What we can do is that we can inherit the employee class functionality to the HR class
For example:
- Protected class Employees {
- Public string Name {
- get;
- set
- }
- }
- Public classs HRemployee: Employees {}
Polymorphism
This means “many forms” and occurs when classes are related through Inheritance.
Polymorphism are of two types,
-
Static/compile time
-
Dynamic/run time
Static polymorphism: achieved by method overloading which means the same method name, but a different signature, for example:
- Public int Add(int a, int b) {
- return (a + b)
- }
- Public int Add(int a, int b, int c) {
- return (a + b + c)
- }
Here, we saw the same method name but a different signature which is static or compile time polymorphism.
Run-time Polymorphism
This can achieve by method overriding which is done through inheritance.
Suppose you have employees class that contains a method called jobrole, and you want to inherit this functionality in the derive class.
This is for inheriting the method to derive class two keywords to be used, such as virtual as well as override Virtual to base class and override to the derived class.
- Abstract class Shape {
- protected int width {
- get;
- set);
-
- Public virtual int Area(int a, int b);
- Public class rectangle: shape {
- Public override int Area(int a, int b) {
- return a * b;
-
- }
- }