BrINClHOF
BrINClHOF

Reputation: 27

How can an instance of a "parent" object use its child object's methods/properties?

It appears that a "Parent" object maintains the "details" of a child object that it's been set to. While this is what I actually want to happen - I don't understand why this is happening, which scares me.

I would expect that when a new parent object is set to a child object, any properties/methods not in parent would be lost/inaccessible. But it seems like if I set it up as below, that's not the case. Can someone explain why this occurs, and if this appropriate to use in this fashion?

(Apologies for a terrible example)

Given the following three classes:

Public Class Person

    Public Property FirstName As String
    Public Property LastName As String

    Public Overridable Function WriteStatement() As String
        Return "[Unknown] " & FirstName & " " & LastName
    End Function

End Class

Public Class Man

    Inherits Person
    Public Property gender As String = "Male"

    Public Overrides Function WriteStatement() As String
        Return "[" & gender & "] " & FirstName & " " & LastName
    End Function

End Class

Public Class Woman

    Inherits Person
    Public Property gender As String = "Female"

    Public Overrides Function WriteStatement() As String
        Return "[" & gender & "] " & FirstName & " " & LastName
    End Function


End Class

And then this executing code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim boy As New Man
Dim girl As New Woman

boy.FirstName = "Scott" : boy.LastName = "Smith"

girl.FirstName = "Jane"
girl.LastName = "Jones"

Dim people As New List(Of Person)
Dim malePerson As New Person
Dim femalePerson As New Person

malePerson = boy
femalePerson = girl

people.Add(malePerson)
people.Add(femalePerson)

For Each p As Person In people
    MsgBox(p.WriteStatement())
Next

'Outputs:
' [Male] Scott Smith
' [Female] Jane Jones
'
'Expected:
' [Unknown] Scott Smith
' [Unknown] Jane Jones

End Sub

Upvotes: 0

Views: 51

Answers (2)

Pierre
Pierre

Reputation: 1046

You should use this in parent class:

public MUSTOVERRIDE class person
Public mustoverride Property gender As String

and in the child class:

Public overrides Property gender As String = "Male"

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

The terms "parent" and "child" are not appropriate in this context. What you're talking about are base types and derived types. You're also wrong when you talk about setting an object to an object. That's just nonsensical. An object is what it is. You can set a variable to an object but that doesn't change what the object is. OOP is designed to mimic real life. If you put a man or a woman where a person is expected, do they stop being a man or a woman? No they don't, and programming objects are the same. If a type overrides a member then calling that member on an object of that type will invoke that derived implementation, whether the type of the reference is the derived type or a base type. That's how overriding works.

Upvotes: 1

Related Questions