Four pillars of Object-Oriented Programming (OOP) in C#
Inheritance
C# is an object-oriented programming (OOP) feature that allows one class to derive properties and behaviors from another class. It promotes code reusability, extensibility and establishes a natural hierarchical relationship between classes.
Types of Inheritance
C# directly supports the following inheritance forms:
- Single Inheritance: One class derives from one base class.
- Multilevel Inheritance: A class derives from another derived class.
- Hierarchical Inheritance: Multiple classes derive from a single base class.
- Multiple Inheritance (Through Interfaces): A class can implement multiple interfaces, achieving multiple inheritance indirectly, since C# does not allow multiple base classes
Encapsulation
Encapsulation is one of the core principles of object-oriented programming (OOP). It refers to the practice of binding data (fields) and the methods that operate on that data into a single unit, while restricting direct access to some components. This ensures controlled interaction with an object’s internal state.
Key Concepts
- Encapsulation hides the internal representation of an object and exposes only necessary operations.
- Fields are often kept private while access is provided through public properties or methods.
- It improves data security, code maintainability and flexibility.
- Access modifiers (private, public, protected, internal) control visibility of members.
Abstraction
a fundamental object-oriented programming (OOP) concept that hides complex implementation details from the user and exposes only the essential features of an object.
Abstraction is the property by which only the essential details are shown to the user and non-essential details or implementations are hidden from the user.
- It reduces the complexity of viewing things.
- Avoids code duplication and increases reusability.
Polymorphism
a core object-oriented programming (OOP) principle that means "many forms", allowing a single interface, method, or object to behave differently depending on the context or the execution state
1. Method Overloading: Multiple methods in the same class share the same name but differ in the number or type of parameters. The correct method is selected by the compiler based on the arguments provided.
Runtime Polymorphism (Method Overriding)
Runtime polymorphism (dynamic polymorphism) in C# is achieved through method overriding. It occurs when a derived class provides a specific implementation of a method already defined in the base class, using the same method name, parameters, and return type. This allows the derived class to modify or extend the behavior of the inherited method.
Key Points:
- Virtual Method: The base class method must be declared as virtual to allow overriding.
- Override Keyword: The derived class method must use the override keyword to provide a new
Comments
Post a Comment