Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I was wondering if I could have the parent class instantiate an object of a child class, without going into an infinite recursion.

Consider this example:

Module Module1
    Sub Main()
        Dim _baseFruit As New Fruit
        _baseFruit.Write()

        Console.ReadKey()
    End Sub
End Module

Public Class Fruit
    Public Property Type As String
    Public Property BaseType As String
    Public Property FruitType As New Apple

    Sub New()
        Type = "Fruit"
        BaseType = "N/A"
    End Sub

    Public Sub Write()
        Console.WriteLine("Type: {0} BaseType: {1} FruitType: {2} ", Type, BaseType, FruitType.Type)
    End Sub
End Class

Public Class Apple
    Inherits Fruit

    Public Sub New()
        Me.Type = "Apple"
    End Sub
End Class

As soon as an Apple is instantiated, it's entering the infinite recursion.

Is it incorrect to say this is impossible, that is to have a child referenced in the parent which is also the base ?

EDIT: From the answer below I have updated the code, lo and behold, it works.

Module Module1
    Sub Main()
        Dim _baseFruit As New Fruit
        Dim _apple As New Apple

        _baseFruit.Write(_apple)
        Console.ReadKey()
    End Sub
End Module

Public Class Fruit
    Public Property Type As String
    Public Property BaseType As String
    Public Property FruitChildInstance As Apple

    Sub New()
        Type = "Fruit"
        BaseType = "N/A"
    End Sub

    Public Sub Write(ByVal fruit As Apple)
        FruitChildInstance = fruit
        Console.WriteLine("Type: {0} BaseType: {1} FruitType: {2} ", Type, BaseType, FruitChildInstance.Type)
    End Sub
End Class

Public Class Apple
    Inherits Fruit

    Public Sub New()
        Me.Type = "Apple"
    End Sub
End Class
share|improve this question

1 Answer

up vote 1 down vote accepted

This could be called an "infinite type". It doesn't have to be a problem if you allow the member variable to be left empty.

Without looking at the actual meaning of this code, you could say this:

Dictating that the FruitType member has to be an Apple instance results in an infinite amount of objects nesting. Leaving it uninitialized (Dim FruitType as Fruit) would cause no problem at all.

Now when you do take the semantic into account:

You're mixing the "Type" concept with the "Instance" concept: a Fruit instance has-a type (e.g. the class Apple), but it's quite strange to let it have-a Fruit member, too.

If you would have modeled this as a class FruitType, where a Fruit has a member dimensioned FruitType, the infinite recursion wouldn't have happened.

share|improve this answer
1  
Very eloquent and easy to understand. I will update the class to account for this. Of course I would argue that if you are doing this you may need to rethink your implementation but never the less a great example of the pitfalls of bad design. – deanvmc Oct 20 '11 at 9:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.