4.1.2.3 - Inheritance, polymorphism and object relationships (VB.NET)

4.1.2.3 - Inheritance, polymorphism and object relationships (VB.NET)

Learn to specialise classes, dispatch overridden methods through a common type and distinguish ownership from inheritance. Interpret UML relationships and apply three object-oriented design principles. The VB.NET route is incomplete: runnable support pending; these are native written source tasks without Run.

Inheritance and overriding

Inheritance defines a derived class as a specialised form of a base class: an is-a relationship. VB.NET uses Inherits and supports one direct base class. A derived object includes its base-class state and can use accessible inherited behaviour. Private base fields remain accessible only within the base class; Protected members are available within derived-class code as well. Ordinary client code cannot access Protected members directly.

VB.NET
Public Class Delivery
    Protected distance As Integer

    Public Sub New(ByVal kilometres As Integer)
        distance = kilometres
    End Sub

    Public Overridable Function Cost() As Integer
        Return distance * 2
    End Function
End Class

Public Class ExpressDelivery
    Inherits Delivery

    Public Sub New(ByVal kilometres As Integer)
        MyBase.New(kilometres)
    End Sub

    Public Overrides Function Cost() As Integer
        Return MyBase.Cost() + 5
    End Function
End Class

Assume small non-negative distances. MyBase.New initialises inherited state using the base constructor; it is the first constructor statement. ExpressDelivery is a Delivery, but specialises its cost. Overriding supplies a replacement implementation with the inherited method's signature. It requires an overridable or abstract base member. MyBase.Cost() deliberately calls the base implementation, avoiding a recursive call to the override itself. The derived code could also read Protected distance; a caller could not.

Inheritance avoids duplication when specialised objects share a genuine contract and implementation. It also creates coupling to the base class. Do not subclass merely to obtain a convenient method when the proposed subclass is not a valid kind of the base class.

Polymorphic dispatch

Polymorphism allows objects of different derived classes to be used through a common base type while responding with their own behaviour. The reference's declared type tells the caller which operations it may request; for an overridable method, the object's actual runtime class selects the implementation.

With the classes above, this is a complete Main body inside a Module:

VB.NET
Sub Main()
    Dim deliveries() As Delivery = {
        New Delivery(2),
        New ExpressDelivery(2)
    }
    For Each item As Delivery In deliveries
        Console.WriteLine(item.Cost())
    Next
End Sub

The first object uses Delivery.Cost and returns 4. The second is reached through a Delivery-typed reference but is an ExpressDelivery object, so its override returns 9. The loop uses one call pattern and does not need a separate type test for each class. A later legitimate Delivery subtype can participate by obeying the same public contract.

The declared base type does not convert an ExpressDelivery object into a plain Delivery or remove its fields. It limits the interface visible through that reference. Methods present only in ExpressDelivery could not be called through the Delivery reference without an appropriate conversion. Overloading is a different idea: it provides multiple signatures with the same name. An overload is not the runtime replacement of one inherited signature. Likewise, Shadows hides a name and should not be substituted for the Overrides contract here.

Aggregation, composition and UML

Aggregation and composition are has-a relationships, where one object contains or refers to another. Aggregation models an independently existing part: a team can refer to a player who exists before joining and may later join another team. Composition models strong ownership: an order owns its order lines, which belong to that order and have no independent role in this model.

[DIAGRAM: asset_slug: vbnet_uml_relationships; description: UML shows Dog inheriting from Animal with a hollow triangle at Animal, Team aggregating Player with a white diamond at Team, and Order composing OrderLine with a black diamond at Order. Access prefixes are public +, private - and protected #.]
Diagram

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account