Object-Oriented Programming (OOP) in C#
Object-Oriented Programming (OOP) in C#
Object-Oriented Programming (OOP) in C# is a programming paradigm that uses objects to design and build applications. It revolves around the following four fundamental principles:
- Encapsulation
Encapsulation is the practice of bundling the data (fields) and methods (functions) that operate on the data into a single unit, called a class. It also involves restricting direct access to some components using access modifiers like private, protected, public, and internal.
- Inheritance
Inheritance allows a class (child class) to inherit fields and methods from another class (parent class). It promotes code reuse and establishes an "is-a" relationship.
- Polymorphism
Polymorphism enables one interface to be used for different types. It can be achieved through
- method overriding (runtime polymorphism)
- method overloading (compile-time polymorphism).
Method Overloading:
- Abstraction
Abstraction focuses on hiding complex implementation details and exposing only the essential features. It is achieved using abstract classes and interfaces.
What is Object-Oriented Programming (OOP)? Explain its principles.
OOP is a programming paradigm based on the concept of "objects," which can contain data in the form of fields (properties) and code in the form of methods.
The four main principles of OOP are:
- Encapsulation: Bundling data and methods into a class and restricting access using access modifiers.
- Inheritance: Allowing a class to inherit properties and methods from another class.
- Polymorphism: Using a single interface to represent different underlying forms (e.g., method overriding and overloading).
- Abstraction: Hiding implementation details and exposing only the essential features.
What is the difference between a class and an object?
- A class is a blueprint or template that defines the structure and behavior of objects. Example:
class Car { } - An object is an instance of a class that is created in memory. Example:
Car myCar = new Car();
What is the difference between method overloading and method overriding?
| Aspect | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Multiple methods in the same class with the same name but different parameters. | A derived class redefines a method of its base class. |
| Compile-time vs Runtime | Compile-time polymorphism. | Runtime polymorphism. |
| Access Modifier | Methods can have any access modifier. | The overriding method must be public, protected, or less restrictive. |
| Example | void Add(int a, int b) void Add(double a, double b) | virtual void Display() override void Display() |
What is an abstract class? How is it different from an interface?
An abstract class is a class that cannot be instantiated and may contain abstract methods (without implementation) and concrete methods (with implementation).
Differences:
| Aspect | Abstract Class | Interface |
|---|---|---|
| Implementation | Can have concrete methods with implementation. | Only method signatures (until C# 8.0, now it can have default methods). |
| Inheritance | Supports single inheritance. | Supports multiple inheritance. |
| Access Modifiers | Can have fields with access modifiers. | Cannot have fields; all members are public by default. |
| Example | abstract class Shape { abstract void Draw(); } | interface IShape { void Draw(); } |
What is the difference between virtual, override, and new keywords in C#?
virtual: Used in the base class to indicate that a method can be overridden in a derived class.override: Used in the derived class to provide a new implementation for avirtualorabstractmethod in the base class.new: Used in the derived class to hide a method from the base class if it is notvirtual.
What is a sealed class?
A sealed class is a class that cannot be inherited. It is declared using the sealed keyword. It is used to prevent further derivation for security or design purposes.
What is the difference between encapsulation and abstraction?
What is the difference between this and base keywords in C#?
this: Refers to the current instance of the class.base: Refers to the parent class of the current class and is used to access base class members.
Can you explain access modifiers in C#?
C# provides the following access modifiers to control the visibility of class members:
public: Accessible from anywhere.private: Accessible only within the containing class.protected: Accessible within the containing class and derived classes.internal: Accessible within the same assembly.protected internal: Accessible within the same assembly or derived classes.private protected: Accessible within the containing class or derived classes in the same assembly.
What is the difference between shallow copy and deep copy?
Answer:
- Shallow Copy: Copies the reference of the objects. Changes in the copied object affect the original object.
- Deep Copy: Copies the entire object along with its dependencies. Changes in the copied object do not affect the original object.
Example of Shallow Copy:
Deep Copy:
Comments
Post a Comment