dwarn
dwarn

Reputation: 13

VB.Net - Implementation on inherited classes

As an example, I have a base class named Animal.

Public MustInherit Class Animal
    Public Property Name As String

      Public Sub New(animalName As String)
        Name = animalName
      End Sub

    Public Sub Sleep()
        MsgBox("ZZZZZZZZZZZ")
    End Sub

    Public MustOverride Sub MakeSound()

End Class

Subclasses can implement one or more of the following interfaces:

Public Interface IShed
    Sub Shed()
End Interface

Public Interface IBeAJerk
    Sub BeAJerk()
End Interface

I have 3 subclasses:

Public Class Dog
    Inherits Animal
    Implements IShed

    Public Sub New(dogName As String)
        MyBase.New(dogName)
    End Sub

    Public Overrides Sub MakeSound()
        MsgBox("Bark")
    End Sub

    Public Sub WagTail()
        MsgBox("Wag")
    End Sub

    Public Sub DogShed() Implements IShed.Shed
        MsgBox("Dog fur everywhere")
    End Sub

End Class

Public Class Cat
    Inherits Animal
    Implements IShed
    Implements IBeAJerk

    Public Sub New(catName As String)
        MyBase.New(catName)
    End Sub

    Public Overrides Sub MakeSound()
        MsgBox("Meow")
    End Sub

    Public Sub Purr()
        MsgBox("Purr")
    End Sub

    Public Sub CatShed() Implements IShed.Shed
        MsgBox("Cat fur everywhere")
    End Sub

    Public Sub Ignore() Implements IBeAJerk.BeAJerk
        MsgBox("Ignore owner")
    End Sub
End Class

Public Class Snake
    Inherits Animal
    Implements IBeAJerk

    Public Sub New(snakeName As String)
        MyBase.New(snakeName)
    End Sub

    Public Overrides Sub MakeSound()
        MsgBox("SSSSSS")
    End Sub

    Public Sub Slither()
        MsgBox("Slither")
    End Sub

    Public Sub Bite() Implements IBeAJerk.BeAJerk
        MsgBox("Bite owner")
    End Sub
End Class

Now I have a method that I want to accept an animal (a dog or a cat) that I know implements IShed. Is there a way to get to the interface without determining which type of animal it is? It would be nice to do something like:

Private Sub MakeAMess(sheddingAnimal As Animal.ThatImplementsIShed)
    'I want to be able to access both IShed (Shed method) and
    'Animal (Name property)
    sheddingAnimal.Shed()
    MsgBox(sheddingAnimal.Name & " made a mess!")
End Sub

I don't want to make the IShed interface into another abstract class because both the Cat and the Snake need to implement the IBeAJerk interface. But the Snake doesn't shed. (Well actually I guess snakes to shed their skin, but you get my point.)

Thanks for any help!

Upvotes: 0

Views: 182

Answers (1)

Igor
Igor

Reputation: 62213

I would recommend creating a new interface IAnimal and then have IShed extend that interface (same goes for IBeAJerk). Type Animal would then implement IAnimal. The method would then take a parameter of type IShed.

Public Interface IAnimal
  Property Name As String
  Sub New(animalName As String)
  Sub Sleep()
  Sub MakeSound()
End Interface

Public Interface IShed
    Inherits IAnimal
    Sub Shed()
End Interface

Public Interface IBeAJerk
    Inherits IAnimal
    Sub BeAJerk()
End Interface

Alternatively you could allow Animal as the parameter to the method and check at runtime if the passed in instance implements IShed and then call Shed

Private Sub MakeAMess(animal As Animal) 
  (TryCast(animal, IShed))?.Shed() 
  ' additional code doing stuff with Animal
End Sub

Upvotes: 1

Related Questions