ReadingNotes

Object Oriented Principles

Inheritance - derive types to create more specialized behavior

Abstract and Sealed Classes and Class Members

  1. Abstract Classes and Class Members:
  1. Sealed Classes and Class Members:

Polymorphism

Polymorphism: Polymorphism is the third pillar of object-oriented programming, alongside encapsulation and inheritance. It refers to the ability of objects of different derived classes to be treated as objects of a common base class. Polymorphism allows for flexibility and extensibility in code by enabling interaction with objects at a higher level of abstraction.

Runtime Polymorphism: At runtime, objects of a derived class can be treated as objects of a base class. This allows for substitutability, as the object’s declared type doesn’t have to be identical to its runtime type. Polymorphism at runtime is useful for method parameters, collections, arrays, and more.

Virtual Methods: Base classes can define virtual methods, which can be overridden by derived classes. Virtual methods have their implementation determined at runtime based on the actual type of the object. This enables derived classes to provide their own implementation while still adhering to the base class’s contract.

Polymorphism in Practice: To demonstrate polymorphism, the article provides an example of a drawing application with different shape classes. A base class called Shape is created with a virtual method Draw(). Derived classes such as Circle, Rectangle, and Triangle override the Draw() method with their specific implementations. A list of Shape objects is created, containing instances of different shape classes. By calling the Draw() method on each shape object, the appropriate overridden implementation is invoked, demonstrating polymorphic behavior.

Virtual Members and Overrides: Virtual members in base classes allow derived classes to define new behavior by overriding them. Derived classes can inherit the closest base class method without overriding it, preserving the existing behavior. Derived classes can also define new non-virtual implementations that hide the base class implementations. The override keyword is used to indicate that a method is intended to participate in virtual invocation.

Hiding Base Class Members: If a derived class wants to have a member with the same name as a member in the base class, the new keyword can be used to hide the base class member. Hidden base class members can still be accessed by casting the derived class instance to the base class type.

Preventing Overriding of Virtual Members: Virtual members can be overridden by derived classes unless the sealed keyword is used to prevent further inheritance. Declaring an override as sealed stops virtual inheritance, and the method is no longer virtual to any class derived from the sealed class.

Accessing Base Class Virtual Members from Derived Classes: A derived class that has overridden a method or property can still access the base class implementation using the base keyword. This allows the derived class to extend the base class’s behavior while still utilizing the base class’s implementation.

Object-Oriented programming (C#)

Things i want to know