Friday, 6 Mar 2026

Mastering Inheritance and Polymorphism in VB.NET OOP

Understanding Inheritance Hierarchies in VB.NET

When building object-oriented applications in VB.NET, inheritance allows classes to inherit properties and methods from parent classes. After analyzing this video tutorial, I've observed that many developers struggle with proper hierarchy design - especially when subclasses inherit inappropriate members. The employee management example demonstrates a classic "is-a" relationship: a CasualEmployee is an Employee, which in turn is a Person. This natural hierarchy enables code reuse but requires careful planning to avoid the salary property pitfall shown in the video.

The Power and Pitfalls of Inheritance

The video clearly shows how subclasses automatically inherit all public members of their superclass. While this creates efficient code reuse (CasualEmployee gains Person's FirstName/LastName and Employee's JobRole), it also exposes a critical design flaw. The original Salary property in Employee class proved problematic because:

  • Permanent employees need annual salary calculation
  • Casual employees require hourly rate computation
    As the developer discovered, you can't selectively inherit properties - subclasses get everything. This is why I recommend always validating property relevance across all potential subclasses during the design phase.

Core Concepts & Authoritative Basis

UML Class Diagrams: The Industry Standard

The video uses UML (Unified Modeling Language) diagrams, the ISO-standard modeling language for software engineering. According to Object Management Group's UML specification, class diagrams should:

  1. Show class names in top compartment
  2. List properties with visibility indicators (+ for public)
  3. Display methods with parameters in bottom compartment
  4. Use arrows to denote inheritance relationships

In our case:

[Person]
+ FirstName : String
+ LastName : String
+ SaveDetails() 
↑
[Employee]
+ JobRole : String
+ RateOfPay : Decimal
+ CalculateMonthlyPay() : Decimal
↑
[CasualEmployee]
+ AgencyContact : String
+ HoursWorked : Integer
+ Bonus() : Decimal
+ Bonus(fixedAmount : Decimal) : Decimal

The three dots (...) indicate omitted properties, following UML's elision standards.

Polymorphism Fundamentals

Microsoft's official VB.NET documentation defines polymorphism as "the ability of objects to behave differently based on their runtime type." The video demonstrates two essential polymorphism types:

  1. Overriding: Subclass replaces superclass method implementation
  2. Overloading: Subclass adds method variations with different signatures

Practical Implementation Guide

Step 1: Designing Inheritable Properties

When creating base classes:

Public Class Employee
    Inherits Person
    
    ' Use generic names for shared properties
    Public Property RateOfPay As Decimal 
    
    ' Mark methods as Overridable
    Public Overridable Function CalculateMonthlyPay() As Decimal
        Return RateOfPay / 12 ' Default annual salary calculation
    End Function
End Class

Pro Tip: Add XML comments to clarify usage:

''' <summary>
''' RateOfPay represents ANNUAL salary for permanent staff 
''' and HOURLY rate for casual staff
''' </summary>

Step 2: Implementing Polymorphism

Overriding Example (CasualEmployee):

Public Class CasualEmployee
    Inherits Employee

    Public Property HoursWorked As Integer

    Public Overrides Function CalculateMonthlyPay() As Decimal
        ' Custom implementation for casual staff
        Return RateOfPay * HoursWorked
    End Function
End Class

Overloading Example:

' In CasualEmployee class
Public Function Bonus() As Decimal
    Return CalculateMonthlyPay() * 0.1D
End Function

' New overload with fixed amount
Public Function Bonus(fixedAmount As Decimal) As Decimal
    Return fixedAmount
End Function

Step 3: Avoiding Common Inheritance Mistakes

  1. The Flawed Salary Property Fix

    • Renamed to RateOfPay for ambiguity reduction
    • Added descriptive tooltips in frontend
    • Alternative approach: Create separate PermanentEmployee class
  2. Validation Checklist Before Inheritance:

    • Will all properties make sense in subclasses?
    • Are method names generic enough?
    • Have I marked overrideable methods?
    • Does my UML diagram reveal hierarchy issues?

Advanced Insights and Best Practices

Beyond the Video: Composition Over Inheritance

While the video uses inheritance, modern OOP often favors composition for payment calculations. Consider:

Public Interface IPayCalculator
    Function CalculatePay() As Decimal
End Interface

Public Class SalaryCalculator
    Implements IPayCalculator
    
    Public Function CalculatePay(annualSalary As Decimal) As Decimal
        Return annualSalary / 12
    End Function
End Class

Public Class HourlyCalculator
    Implements IPayCalculator
    
    Public Function CalculatePay(hourlyRate As Decimal, hours As Integer) As Decimal
        Return hourlyRate * hours
    End Function
End Class

This approach avoids the inheritance dilemma entirely while maintaining polymorphism through interface implementation.

Performance Considerations

When overriding methods:

  • Avoid complex logic in frequently called methods
  • Use NotOverridable for performance-critical methods
  • Profile using Visual Studio's diagnostic tools

Actionable Resource Guide

Immediate Implementation Checklist

  1. Audit existing class hierarchies for inappropriate properties
  2. Convert specific properties to generic names (e.g., Salary → RateOfPay)
  3. Add Overridable to methods needing subclass customization
  4. Implement at least one override demonstration
  5. Create UML diagram for visual verification

Recommended Tools

  1. Visual Studio Class Designer (Built-in UML tool)
    • Why: Directly generates code from diagrams
  2. PlantUML (Text-based diagramming)
    • Why: Version-control friendly syntax
  3. NDepend (Code analysis)
    • Why: Detects inheritance hierarchy issues

Conclusion

Inheritance and polymorphism form the backbone of effective VB.NET OOP, as demonstrated through the employee management case study. Remember that while inheritance enables powerful code reuse, it requires careful upfront design to avoid the property inheritance pitfalls shown. True polymorphism mastery comes from strategically implementing both method overriding and overloading based on your specific domain needs.

When implementing inheritance in your projects, which design challenge do you anticipate being most difficult? Share your experience in the comments below!