VB.NET: Object-Oriented Programming: Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to define a new class that inherits properties and methods from an existing class. This promotes code reusability and establishes a relationship between classes, often referred to as a "is-a" relationship.

What is Inheritance?

In VB.NET, inheritance is implemented using the `Inherits` keyword. A class that inherits from another class is called a derived class (or child class), and the class it inherits from is called the base class (or parent class).

The derived class automatically gains access to all public and protected members (fields, properties, methods, events) of its base class. Private members of the base class are not directly accessible by the derived class.

Syntax

To inherit from a base class, you use the Inherits keyword:


' Base Class
Public Class Vehicle
    Public Property Make As String
    Public Property Model As String

    Public Sub StartEngine()
        Console.WriteLine("Engine started.")
    End Sub
End Class

' Derived Class
Public Class Car
    Inherits Vehicle

    Public Property NumberOfDoors As Integer

    Public Sub Drive()
        Console.WriteLine($"Driving the {Make} {Model}.")
    End Sub
End Class
            

Key Concepts

Overriding Members

Derived classes can provide their own specific implementation for methods inherited from the base class. This is known as overriding. To enable overriding, the base class method must be declared with the Overridable keyword, and the derived class method must be declared with the Overrides keyword.

Note: Methods marked as NotOverridable in the base class cannot be overridden by derived classes.

' Base Class
Public Class Shape
    Overridable Function CalculateArea() As Double
        Return 0.0
    End Function
End Class

' Derived Class 1
Public Class Circle
    Inherits Shape

    Public Property Radius As Double

    Public Overrides Function CalculateArea() As Double
        Return Math.PI * Radius * Radius
    End Function
End Class

' Derived Class 2
Public Class Rectangle
    Inherits Shape

    Public Property Width As Double
    Public Property Height As Double

    Public Overrides Function CalculateArea() As Double
        Return Width * Height
    End Function
End Class
            

Shadowing Members

If a derived class declares a member with the same name as a member in the base class, but the base class member is not marked as Overridable, the derived class member shadows the base class member. Shadowing hides the base class member rather than providing a different implementation of the same member.


' Base Class
Public Class Employee
    Public Salary As Decimal = 50000
End Class

' Derived Class
Public Class Manager
    Inherits Employee

    ' This Shadows the Salary member in the Employee class
    Public Shadows Salary As Decimal = 75000

    Public Sub DisplaySalary()
        ' Accesses the Manager's Salary
        Console.WriteLine($"Manager Salary: {Salary}")
        ' Accesses the Employee's Salary using MyBase
        Console.WriteLine($"Base Employee Salary: {MyBase.Salary}")
    End Sub
End Class
            

MyBase and MyClass

VB.NET provides keywords to explicitly refer to members of the base class:

Single Inheritance

VB.NET supports single inheritance for classes. This means a class can inherit from only one direct base class. However, a class can inherit indirectly through a chain of classes.

Tip: To achieve behavior similar to multiple inheritance, you can use interfaces.

Benefits of Inheritance

Abstract Classes and Methods

You can declare classes and methods as MustInherit and MustOverride, respectively. Abstract classes cannot be instantiated directly; they are intended to be inherited from. Abstract methods must be implemented by all non-abstract derived classes.


' Abstract Base Class
Public MustInherit Class Mammal
    Public MustOverride Sub MakeSound()

    Public Sub Breathe()
        Console.WriteLine("Breathing...")
    End Sub
End Class

' Concrete Derived Class
Public Class Dog
    Inherits Mammal

    Public Overrides Sub MakeSound()
        Console.WriteLine("Woof!")
    End Sub
End Class
            
Important: A class that inherits from an abstract class must either implement all its abstract members or be declared as abstract itself.

Sealed Classes and Methods

You can use the NotInheritable keyword to create a sealed class, which cannot be inherited from. Similarly, NotOverridable can be used on a method in a base class to prevent it from being overridden.


Public NotInheritable Class FinalClass
    ' This class cannot be inherited.
End Class

Public Class BaseWithSealedMethod
    Public NotOverridable Sub DoSomethingImportant()
        ' ...
    End Sub
End Class
            

Conclusion

Inheritance is a powerful OOP feature in VB.NET that significantly enhances code organization, reusability, and extensibility. Understanding how to properly use inheritance, overriding, and shadowing is crucial for building robust and maintainable applications.