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.
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:
MyBase: Refers to the immediate base class of the current class. It's used to call methods or access members defined in the base class, especially when overriding or shadowing.MyClass: Refers to the current class itself. It's used to call methods or access members of the current class, often to disambiguate when a member name is the same as a base class member.
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.
Benefits of Inheritance
- Code Reusability: Avoids redundant code by defining common functionalities in a base class.
- Extensibility: Allows you to create new classes that build upon existing ones without modifying the original code.
- Polymorphism: Enables objects of derived classes to be treated as objects of their base class, leading to flexible and dynamic code.
- Maintainability: Changes made to the base class are automatically reflected in all derived classes, simplifying maintenance.
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
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.