Classes
.NET Inheritance
Class Inheritance
.NET inheritance uses : for base class extension.
Understanding Inheritance in .NET
Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and methods from another class. In .NET, inheritance is achieved using the :
symbol followed by the base class name.
This feature promotes code reusability and establishes a hierarchical relationship between classes, enabling derived classes to extend the functionality of base classes.
Basic Inheritance Syntax
To create a derived class that inherits from a base class in .NET, use the following syntax:
Example: Implementing Inheritance
Let's look at a practical example of how inheritance works in .NET:
Overriding Methods in Inheritance
In .NET, derived classes can override methods of the base class to provide specific implementations. To override a method, the base method must be marked with the virtual
keyword, and the overriding method must use the override
keyword.
Sealed Classes and Methods
In some cases, you might want to prevent further inheritance of a class or override of a method. This can be achieved using the sealed
keyword. When applied to a class, it prevents the class from being inherited. When applied to a method, it prevents the method from being overridden further.
Classes
- Classes
- Structs
- Records
- Interfaces
- Inheritance
- Abstract Classes
- Enums
- Properties
- Previous
- Interfaces
- Next
- Abstract Classes