Why do we use Oops?
- Oops, it stands for Object-Oriented Programming, which helps the developer easily control and access a program's data.
- Oops, it helps developer to write code in a better way and improves code readability and reusability.
- Oops, it keeps our code DRY "don't repeat yourself" and makes the code easier to maintain, modify and debug.
- It helps to create a clear structure for your application.
What are Class and Objects?
- Class is a blueprint. Class may have field, property, method, delegate, and events.
- The object is an instance of the class, and using the object, you can access all class members, like field, property, method, etc.
What are Access Modifiers?
Public
Accessible by anyone like the same class, derived class, current assembly, and another assembly. Please refer below example. I have created public property with a name and then a Product Base class inherited from the product class. I have created a product class object and accessed that property in the main method. It means public property accessible by anyone in the same class, derived class, current assembly, and another assembly.
Private
Access is limited to the class which contains it. If the variable declares private in a class, then accessible in that class only, the user can not access outside from that class. Please check the below example, Base class ID property is marked private, then that class is inherited in another class and then created object of that class, but the property ID is not accessible in that class.
Protected
When we declare a member protected, it can be accessed only within the same or derived class. It can not be accessed by the object of that class.

Now let's inherit the Product class into the Program class. If we want to access a protected member, we need to create an object of the derived class, and then we can access a protected member. I have made a member name with protected access modifier and accessed it via the program class object,t as in the snippet below.

Internal
Internal can be visible inside the same assembly and accessible through an object. It can not be accessible outside of assembly, either object or inheritance.
Protected Internal
Protected Internal can be visible inside the same assembly through objects and in derived classes outside the assembly through member functions.
Below is a pictorial to represent the access modifier, which helps us understand easily. Here have created a snippet that indicates which access modifier has accessibility.

4. What is the main pillar of Object-Oriented Programming?
There are four main pillars of Object-oriented programming,
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
5. What are Abstraction and Encapsulation?
Abstraction
- Abstraction means to show only what is necessary. In other words, show only necessary features without background details.
- Abstraction can be achieved by using access modifiers, abstract classes, and interfaces.
- It solved a problem at the design level
Encapsulation
- Encapsulation is the process of wrapping several items like variables, methods, and functions in a single entity, making the system easier to handle and use for the end-user.
- Encapsulation can be achieved by using classes.
6. Inheritance
- Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows the developer to reuse the functionality of the base class and speed up development time.
- Inheritance is the process of deriving a new class from an existing class and available all base class functionality in the derived class.
7. What is Polymorphism?
- Polymorphism is one of the most important concepts of object-oriented programming. It describes a single name with multiple behaviors, which means a single function can be defined with different definitions.
- There are two types of polymorphisms, one is compiled time polymorphism (Method Overloading), and another is run time polymorphism (Method Overriding)
- We can call it another name, like static polymorphism (Method Overloading) and dynamic polymorphism (Method Overriding).
8. What is Method Overloading?
Method overloading is compiled time polymorphism. Method overloading means the method name is the same, but the parameter may differ. Let's take the example of method overloading below,
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
namespace OOPSQuestions {
class Program {
static void Main(string[] args) {
//Get All product without parameter
var result = GetProducts();
//Get products with price greater than 325
var productWithPrice = GetProducts(325);
//Get products with name contains shirt
var productWithName = GetProducts("Shirt");
//Get products with name start H and price greater than 325
var productWithNameAndPrice = GetProducts("H", 325);
}
public static List < Product > GetProducts() {
List < Product > products = BuildProduct();
return products;
}
public static List < Product > GetProducts(decimal price) {
List < Product > products = BuildProduct().Where(x => x.price > price).ToList();
return products;
}
public static List < Product > GetProducts(string Name) {
List < Product > products = BuildProduct().Where(x => x.Name.Contains(Name)).ToList();
return products;
}
public static List < Product > GetProducts(string Name, decimal price) {
List < Product > products = BuildProduct().Where(x => x.Name.StartsWith(Name) && x.price > price).ToList();
return products;
}
public static List < Product > BuildProduct() {
var list = new List < Product > () {
new Product() {
Id = 1,
Name = "T Shirt",
Code = "PTS",
Description = "Polyster Tshirt",
price = 300
},
new Product() {
Id = 2,
Name = "Full Sleev Shirt",
Code = "PSS",
Description = "cotton Full Sleev Shirt",
price = 350
},
new Product() {
Id = 3,
Name = "Hoodies",
Code = "PHD",
Description = "Polyster Hoodies",
price = 400
},
new Product() {
Id = 4,
Name = "Half Sleev Shirt",
Code = "HSS",
Description = "Half Sleev Shirt",
price = 350
}
};
return list;
}
}
}
public class Product {
public int Id {
get;
set;
}
public string Name {
get;
set;
}
public string Code {
get;
set;
}
public string Description {
get;
set;
}
public decimal price {
get;
set;
}
}
In the above example, I created the GetProduct() method with the same name but different parameters.
- First method without parameter - GetProducts()
- The second method with one parameter as decimal type - GetProducts(decimal price)
- The third method takes string parameter - GetProducts(string Name)
- The fourth one takes two parameters, string and decimal - GetProducts (string Name, decimal price
Note. The method's overloading parameters may differ, but the return type must be the same.
9. What is Method Overriding?
Overriding means overriding the base class method in a derived class with different logic but with the same name and same parameter. While creating the method in the base class, we need to mark a method as virtual, and in a derived class, we need to mark it with the override keyword. Let's take an example of method overriding,

Pankajkumar PatelPosted May 5, 2023, 4:10 AM
Well explained with example.
Mahesh ChandPosted May 4, 2023, 12:21 PM
Nice Jignesh. Thank you!