LibraryInheritance

Inheritance

Learn about Inheritance as part of C# .NET Development and Azure Integration

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.

What is the primary benefit of using inheritance in C#?

Code reusability.

Syntax for Inheritance

In C#, you use a colon (

code
:
) followed by the base class name to indicate that a class inherits from another class. The base class is specified immediately after the derived class name in the class declaration.

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:

  • code
    public
    : Accessible from anywhere.
  • code
    protected
    : Accessible within the base class and by derived classes.
  • code
    private
    : Accessible only within the base class itself.
  • code
    internal
    : Accessible within the same assembly.
  • code
    protected internal
    : Accessible within the same assembly or by derived classes in different assemblies.
  • code
    private protected
    : Accessible within the same assembly and by derived classes within that assembly.
ModifierAccessible in Base ClassAccessible in Derived ClassAccessible Outside Assembly
publicYesYesYes
protectedYesYesNo
privateYesNoNo
internalYesYes (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

code
virtual
or
code
abstract
keyword, and the derived class method must be marked with the
code
override
keyword. This is a key mechanism for achieving polymorphism.

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

code
AzureResource
with common properties like
code
ResourceId
and
code
Location
. Then, derived classes like
code
AzureVirtualMachine
,
code
AzureBlobStorage
, or
code
AzureFunctionApp
can inherit these common properties and add their specific attributes and behaviors. This promotes a structured and maintainable approach to managing and interacting with various Azure resources through your C# code.

What keyword must be used in the derived class to implement a specific version of a base class's virtual method?

override

Learning Resources

Inheritance (C# Programming Guide)(documentation)

The official Microsoft documentation provides a comprehensive overview of inheritance in C#, covering syntax, access modifiers, and best practices.

Object-Oriented Programming in C#(tutorial)

This tutorial covers the core concepts of OOP in C#, including inheritance, with clear explanations and code examples.

C# Inheritance Explained(video)

A visual explanation of C# inheritance, demonstrating how to create base and derived classes and the concept of method overriding.

Understanding Inheritance in C#(blog)

A blog post that delves into the nuances of C# inheritance, including practical examples and common pitfalls to avoid.

Polymorphism in C#(blog)

Explains polymorphism in C#, a concept closely tied to inheritance, focusing on how derived classes can be treated as their base class.

C# Virtual, Override, and Abstract Methods(tutorial)

Details the use of `virtual`, `override`, and `abstract` keywords, which are essential for implementing inheritance effectively.

C# Inheritance - W3Schools(tutorial)

A beginner-friendly introduction to C# inheritance with interactive examples and clear explanations of the syntax and concepts.

Introduction to Object-Oriented Programming(wikipedia)

Provides a foundational understanding of object-oriented programming principles, including inheritance, from a broader computer science perspective.

C# Access Modifiers(tutorial)

A guide to understanding C# access modifiers and how they affect the visibility and accessibility of class members, crucial for inheritance.

Azure SDK for .NET(documentation)

Explore the Azure SDK for .NET to see how C# classes and inheritance can be applied to interact with various Azure services.