Inheritance: The article explains that inheritance allows you to create new classes that reuse, extend, and modify the behavior defined in other classes. It mentions that a derived class can have only one direct base class but can inherit members from multiple levels of inheritance.
Abstract and virtual methods: The article describes how virtual methods can be overridden by derived classes, providing their own implementation. It also explains that abstract methods must be overridden in non-abstract classes that directly inherit from the abstract class. Abstract and virtual methods enable polymorphism.
Abstract base classes: The article introduces abstract classes, which cannot be instantiated directly but can be used as a base for derived classes. Abstract classes can contain abstract methods that must be implemented by derived classes. It mentions that derived classes that are not abstract must provide implementations for abstract methods.
Interfaces: The article explains that interfaces define a set of members that must be implemented by classes or structs that implement the interface. It mentions that a class can implement multiple interfaces but can only derive from a single direct base class.
Preventing further derivation: The article mentions that a class or member can be marked as sealed to prevent other classes from inheriting from it. Sealed classes cannot be used as base classes.
Derived class hiding of base class members: The article explains that a derived class can hide base class members by declaring members with the same name and signature. It introduces the new modifier, which can be used to explicitly indicate that the member is not intended to be an override of the base member.
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.