null null
null null

Reputation: 63

Abstract class using an interface impleented in subclass?

I have an idea that uses both an abstract class and an interface, but i'm not sure if its possible to do what I am thinking of and how.

I have an abstract class modeling a device, containing only abstract functionality. When implemented this class basically serves as a driver for the specific device.

Each device may have different features, I am attempting to model the feature sets as different interfaces that may or may not be implemented by the class designer.

Is there a mechanism for me to determine if a subclass of the device class is implementing these interfaces. I have to determine it FROM the super class, then be able to call the functions defined in the subclass from there.

This sounds impossible to me, but I'm just curious if someone else has more intuition, or possibly a better solution.

In the example below I've illustrated my point. I would like to be able ot have an object of type device, and call the functions implemented in the subclass through some mechanism.

Thanks!

Public MustInherit Class Device
   Public MustOverride Sub One()

   Public Function SupportsBonding As Boolean
      'Returns true if subclass implments interface
   End Function
End Class

Public Interface Bonding
   Sub Two()
   Sub Three()
End Interface

Public Class Device1
    Inherits Device
    Implements Bonding 

    Public Sub Two()
    End Sub

    Public Sub Three()
    End Sub
End Class

Upvotes: 0

Views: 44

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239734

You can always use a TypeOf operator using the Me keyword, something like:

If TypeOf Me Is IAmSomeInterface Then
     ...
End If

Even if this code is running in the superclass, it will always work against the runtime type of the object, so you'll get the subclass information.

Or, if you're planning to call methods on the interface, you might use a TryCast instead:

Dim someObject = TryCast(Me,IAmSomeInterface)
If Not someObject Is Nothing Then
     someObject.DoSomething()
End If

Upvotes: 1

Related Questions