Sougata
Sougata

Reputation: 337

Base Class Reference to Derived Class Object

Want to understand which of the following cases can be interpreted as an example of Base Class Referencing to a Derived Class Object. My understanding is that CASE 1 is indeed an example of the base class reference to derived class object. But i am confused with Case 2. I share the reasons behind my understanding below. Requesting your expert opinion on this:

CASE 1: In the following example, both "b" and "new_b" are "Base" class type variables which holds the reference(memory address) to the derived class object "d". Hence I am interpreting both the definitions as a case of Base Class Reference to derived class object. Is my understanding correct?

Module Module1
    Sub Main()
        Dim d As New Derived()
        '2 examples of base class reference to a derived class object
        Dim b As Base = DirectCast(d, Base)
        Dim new_b As Base = New Derived()
    End Sub
End Module

Public Class Base
    'Code Block
End Class

Public Class Derived
    Inherits Base
    'Code Block
End Class

CASE 2: Code mentioned below -- When the Me.testMethod() line gets executed in the useMe procedure in the base class, the ME refers to the current instance testObj which is of the derivedClass type. I am confused as to whether this can be interpreted as a case of base class reference to a derived class object. The key point of confusion is the following: Just because ME is written inside the base class, whether to interpret ME as a base class type variable (something similar to the variables "b" and "new_b") that holds reference to the testObj which is a derived class object?

Module Module1
    Sub Main()
        Dim testObj As New derivedClass()
        Console.Write("Using Me Keyword:       ")
        testObj.useMe()
        Console.Write("Using MyClass Keyword:  ")
        testObj.useMyclass()
        Console.ReadLine()
    End Sub
End Module

Public Class baseClass
    Public Overridable Sub testMethod()
        Console.WriteLine("Base Class String")
    End Sub

    Public Sub useMe()
        Me.testMethod() 'Does this line represent a base class ref to a derived class obj?
    End Sub
    
End Class


Class derivedClass : Inherits baseClass
    Public Overrides Sub testMethod()
        Console.WriteLine("Derived Class String")
    End Sub
End Class


Upvotes: 1

Views: 498

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

In your case 1 in the question, yes it is an example of a base type reference to a derived type instance. It's pretty simple stuff. A common example is a Control variable referring to any type of control, e.g. a Button or a TextBox. Let's say that you have a method that handles an event for a Button and for a TextBox. You could cast the sender parameter as type Control and it would work for both. You could then get the Text property of the control via the reference, because Text is a member of the Control class. It would work exactly the same way regardless of the type of the control.

Your second case isn't relevant. You're talking about code inside one of the classes. Talk of a base type reference to a derived type object is only relevant outside both types. You declare a variable of the base type and assign an object of the derived type to it. If you're not doing that then you're not doing a base type reference to a derived type object.

That second code is just about the difference between Me and MyClass. For the record, I've been coding in VB.NET for almost 20 years and I have never once used MyClass, so I would suggest that you don't lose much sleep over it. Also, your second code snippet calls a method that doesn't exist in your code, so that's a problem.

Upvotes: 2

Related Questions