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!