C# Inheritance: Building on Existing Code
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to define a new class based on an existing class. This promotes code reusability and establishes a hierarchical relationship between classes, often referred to as a 'is-a' relationship. In C#, the derived class (child class) inherits members (fields, properties, methods) from the base class (parent class).
Key Concepts of Inheritance
Inheritance enables code reuse and establishes 'is-a' relationships.
A derived class can inherit accessible members from its base class, reducing the need to rewrite code. For example, a Car
class might inherit from a Vehicle
class, sharing common properties like Speed
and Engine
.
The core principle of inheritance is to allow a new class (derived class) to reuse, extend, and modify the behavior defined in an existing class (base class). This creates a class hierarchy. The derived class automatically gets all the public and protected members of the base class. Private members of the base class are not directly accessible by the derived class, but they are still part of the base class's functionality.
Code reusability.
Syntax for Inheritance
In C#, you use a colon (
:
Consider a Shape
base class with a CalculateArea()
method. A Circle
class can inherit from Shape
and override CalculateArea()
to provide its specific implementation. This demonstrates how derived classes can specialize behavior. The syntax class DerivedClass : BaseClass
is crucial for establishing this relationship.
Text-based content
Library pages focus on text content
Access Modifiers and Inheritance
The accessibility of inherited members depends on their access modifiers in the base class:
- : Accessible from anywhere.codepublic
- : Accessible within the base class and by derived classes.codeprotected
- : Accessible only within the base class itself.codeprivate
- : Accessible within the same assembly.codeinternal
- : Accessible within the same assembly or by derived classes in different assemblies.codeprotected internal
- : Accessible within the same assembly and by derived classes within that assembly.codeprivate protected
Modifier | Accessible in Base Class | Accessible in Derived Class | Accessible Outside Assembly |
---|---|---|---|
public | Yes | Yes | Yes |
protected | Yes | Yes | No |
private | Yes | No | No |
internal | Yes | Yes (in same assembly) | Yes (in same assembly) |
Method Overriding
Method overriding allows a derived class to provide a specific implementation for a method that is already defined in its base class. To override a method, the base class method must be marked with the
virtual
abstract
override
Think of virtual
as a suggestion from the base class to the derived class: 'You can change this behavior if you need to.' override
is the derived class accepting that suggestion.
Inheritance and Azure Integration
In the context of Azure integration, inheritance can be used to model relationships between different Azure services or components. For instance, you might have a base class representing a generic
AzureResource
ResourceId
Location
AzureVirtualMachine
AzureBlobStorage
AzureFunctionApp
override
Learning Resources
The official Microsoft documentation provides a comprehensive overview of inheritance in C#, covering syntax, access modifiers, and best practices.
This tutorial covers the core concepts of OOP in C#, including inheritance, with clear explanations and code examples.
A visual explanation of C# inheritance, demonstrating how to create base and derived classes and the concept of method overriding.
A blog post that delves into the nuances of C# inheritance, including practical examples and common pitfalls to avoid.
Explains polymorphism in C#, a concept closely tied to inheritance, focusing on how derived classes can be treated as their base class.
Details the use of `virtual`, `override`, and `abstract` keywords, which are essential for implementing inheritance effectively.
A beginner-friendly introduction to C# inheritance with interactive examples and clear explanations of the syntax and concepts.
Provides a foundational understanding of object-oriented programming principles, including inheritance, from a broader computer science perspective.
A guide to understanding C# access modifiers and how they affect the visibility and accessibility of class members, crucial for inheritance.
Explore the Azure SDK for .NET to see how C# classes and inheritance can be applied to interact with various Azure services.