locked
General info on the INTERFACE keyword please. RRS feed

  • Question

  • Hi,

    If you have a separately declared PUBLIC INTERFACE and then IMPLEMENT that INTERFACE.

    1) My general question: Is an INTERFACE simply a means to ensure that a CLASS implements all the methods defined in an interface
     and to ensure that they can only be implemented once?

    I have noticed that a method that IMPLEMENTS an INTERFACE method can still have overloads though. :-) 

    If so, when combined with INHERITANCE though I believe I can finally see the use for defining an INTERFACE!!

    2) Do you have to use an interface when linking to a VB6 project ( is this via Component Object Model  or COM ) and if so,
     do any attibutes need to be applied when doing so?

    Here is a code snippet.

    Opinions please on creating a PUBLIC INTERFACE outside of any CLASS.
     I guess this would not normally be done from an OOP point of view?


    Public Class ExampleClass
        Implements MyActions
        Implements Whatever
    
        Private Interface Whatever
            Sub DoSomething()
        End Interface
    
        Public Sub Eat() Implements MyActions.Eat
    
        End Sub
    
        Public Sub Jog() Implements MyActions.Jog
    
        End Sub
    
        Public Sub Sleep() Implements MyActions.Sleep
    
        End Sub
    
        Public Sub Walk() Implements MyActions.Walk
    
        End Sub
    
        Public Sub DoSomething() Implements Whatever.DoSomething
    
        End Sub
    
    End Class
    
    Public Class AnotherClass
        Inherits ExampleClass
    
    End Class
    
    Public Interface MyActions
    
        Sub Eat()
        Sub Jog()
        Sub Sleep()
        Sub Walk()
    
    End Interface



    By the way in case you haven't guessed, I have not previously used INTERFACE definitions in the past.

    I know what INHERITANCE, POLYMORPHISM and ENCAPSULATION are though so I know OOP to some degree. I
     just haven't created a very big project yet!!

    :-D

    Thank you for your time.   ;-]    :-D


    Regards,

    John
    Tuesday, January 5, 2010 5:41 PM

Answers

  • Hi John,

    I don't see any advantage gained by your private nested interface.  Nested types are usually created for the exclusive use of the surrounding type.  The first example that comes to mind are the nested classes inside of many Forms controls that define their collection items.  For example, System.Windows.Forms.ComboBox.ObjectCollection is a nested class inside of the class ComboBox.  A seldom realized fact about nested classes is that they have full access to members of the containing class, both public and private members.

    Back to your interfaces.  One mantra of OOP is this, "Favor composition over inheritance".  What that means is avoid the trap of depending upon inheritance for too much of your base functionality in your classes.  This is where interfaces come into play.  An interface is a contract that defines the "signature", if you will, of a class.

    An interface specifies what class members a type must have.  It is also a good practice to prefix their name with the capital letter I, as in IDisposable.   The power of interfaces is realized through Polymorphism and Abstraction.  When you do not specify types and specific classes within your code, then your code becomes more flexible and is able to work with any type that implements the interfaces.

    I wrote a sample Console Application of this. 
    Note how the methods can work with any type.
    I borrowed your interface, too.  ... sort of ...

    Module Module1

        Sub Main()
            Dim creature As Animal
            '
            ' Test the Dog Class
            '
            creature = New Dog()
            Eat(creature)
            Sleep(creature)
            Walk(creature)
            '
            ' Test the Cat Class
            '
            creature = New Cat()
            Eat(creature)
            Sleep(creature)
            Walk(creature)
            '
            Console.ReadLine()
        End Sub

        Private Sub Eat(ByVal creature As IActions)
            creature.Eat()
        End Sub
        Private Sub Sleep(ByVal creature As IActions)
            creature.Sleep()
        End Sub
        Private Sub Walk(ByVal creature As IActions)
            creature.Walk()
        End Sub

    End Module

    Public Interface IActions
        Sub Eat()
        Sub Sleep()
        Sub Walk()
    End Interface

    MustInherit Class Animal : Implements IActions
        Protected animalTypeName As String

        Public Sub New()
            Me.animalTypeName = GetTypeName()
        End Sub

        Protected MustOverride Function GetTypeName() As String

        Public MustOverride Sub Eat() Implements IActions.Eat

        Public Overridable Sub Sleep() Implements IActions.Sleep
            Console.WriteLine("The {0} sleeps.", Me.animalTypeName)
        End Sub

        Public Overridable Sub Walk() Implements IActions.Walk
            Console.WriteLine("The {0} walks.", Me.animalTypeName)
        End Sub
    End Class

    Class Dog : Inherits Animal

        Protected Overrides Function GetTypeName() As String
            Return Me.GetType().Name
        End Function
        Public Overrides Sub Eat()
            Console.WriteLine("The {0} eats sloppily.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Sleep()
            Console.WriteLine("The {0} sleeps noisily.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Walk()
            MyBase.Walk()
        End Sub

    End Class

    Class Cat : Inherits Animal

        Protected Overrides Function GetTypeName() As String
            Return Me.GetType().Name
        End Function

        Public Overrides Sub Eat()
            Console.WriteLine("The {0} eats very neatly.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Sleep()
            Console.WriteLine("The {0} sleeps very quietly.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Walk()
            MyBase.Walk()
        End Sub

    End Class



    Hope this helps.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, January 5, 2010 6:37 PM
  • Not really sure what you mean by "linking" to a VB 6.0 project but COM/OLE automation requires an interface; it's just that the interface is automatically built into the COM implementation by default when using Visual Basic 6.0. You can create interfaces for Visual Basic 6.0 COM components for the purpose of abstraction, but it's not typical.

    With respect to inheritance, Visual Basic 6.0 only truly supports implementation inheritance and not interface inheritance.

    Edit: Additional info:

    How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, January 5, 2010 6:45 PM

All replies

  • Hi John,

    I don't see any advantage gained by your private nested interface.  Nested types are usually created for the exclusive use of the surrounding type.  The first example that comes to mind are the nested classes inside of many Forms controls that define their collection items.  For example, System.Windows.Forms.ComboBox.ObjectCollection is a nested class inside of the class ComboBox.  A seldom realized fact about nested classes is that they have full access to members of the containing class, both public and private members.

    Back to your interfaces.  One mantra of OOP is this, "Favor composition over inheritance".  What that means is avoid the trap of depending upon inheritance for too much of your base functionality in your classes.  This is where interfaces come into play.  An interface is a contract that defines the "signature", if you will, of a class.

    An interface specifies what class members a type must have.  It is also a good practice to prefix their name with the capital letter I, as in IDisposable.   The power of interfaces is realized through Polymorphism and Abstraction.  When you do not specify types and specific classes within your code, then your code becomes more flexible and is able to work with any type that implements the interfaces.

    I wrote a sample Console Application of this. 
    Note how the methods can work with any type.
    I borrowed your interface, too.  ... sort of ...

    Module Module1

        Sub Main()
            Dim creature As Animal
            '
            ' Test the Dog Class
            '
            creature = New Dog()
            Eat(creature)
            Sleep(creature)
            Walk(creature)
            '
            ' Test the Cat Class
            '
            creature = New Cat()
            Eat(creature)
            Sleep(creature)
            Walk(creature)
            '
            Console.ReadLine()
        End Sub

        Private Sub Eat(ByVal creature As IActions)
            creature.Eat()
        End Sub
        Private Sub Sleep(ByVal creature As IActions)
            creature.Sleep()
        End Sub
        Private Sub Walk(ByVal creature As IActions)
            creature.Walk()
        End Sub

    End Module

    Public Interface IActions
        Sub Eat()
        Sub Sleep()
        Sub Walk()
    End Interface

    MustInherit Class Animal : Implements IActions
        Protected animalTypeName As String

        Public Sub New()
            Me.animalTypeName = GetTypeName()
        End Sub

        Protected MustOverride Function GetTypeName() As String

        Public MustOverride Sub Eat() Implements IActions.Eat

        Public Overridable Sub Sleep() Implements IActions.Sleep
            Console.WriteLine("The {0} sleeps.", Me.animalTypeName)
        End Sub

        Public Overridable Sub Walk() Implements IActions.Walk
            Console.WriteLine("The {0} walks.", Me.animalTypeName)
        End Sub
    End Class

    Class Dog : Inherits Animal

        Protected Overrides Function GetTypeName() As String
            Return Me.GetType().Name
        End Function
        Public Overrides Sub Eat()
            Console.WriteLine("The {0} eats sloppily.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Sleep()
            Console.WriteLine("The {0} sleeps noisily.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Walk()
            MyBase.Walk()
        End Sub

    End Class

    Class Cat : Inherits Animal

        Protected Overrides Function GetTypeName() As String
            Return Me.GetType().Name
        End Function

        Public Overrides Sub Eat()
            Console.WriteLine("The {0} eats very neatly.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Sleep()
            Console.WriteLine("The {0} sleeps very quietly.", Me.animalTypeName)
        End Sub

        Public Overrides Sub Walk()
            MyBase.Walk()
        End Sub

    End Class



    Hope this helps.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, January 5, 2010 6:37 PM

  • I have noticed that a method that IMPLEMENTS an INTERFACE method can still have overloads though. :-) 



    The interface only ensure the signatures, so you can make overloads
    Tuesday, January 5, 2010 6:43 PM
  • Not really sure what you mean by "linking" to a VB 6.0 project but COM/OLE automation requires an interface; it's just that the interface is automatically built into the COM implementation by default when using Visual Basic 6.0. You can create interfaces for Visual Basic 6.0 COM components for the purpose of abstraction, but it's not typical.

    With respect to inheritance, Visual Basic 6.0 only truly supports implementation inheritance and not interface inheritance.

    Edit: Additional info:

    How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, January 5, 2010 6:45 PM
  • Hi Rudedog2,

    Thanks for your code it has helped me to understand INTERFACEs a lot better.

    To Paul P Clement IV MVP,

    Can you expand on what you mean by "implementation inheritance" please?

    Do you mean VB6 only INHERITS "regular" methods such as.>>



    Public Sub DoSometing()

    End Sub

    Public Function Whatever() As String

    Return "Whatever!!"

    End Function



    Thanks for answering my question 2)


    Regards,

    John
    Wednesday, January 6, 2010 1:30 AM
  • Actually, I need to correct my previous statement. Classic Visual Basic (6.0 and earlier) only supports (Implements) Interface inheritance through a Class. There is no distinction between a Class and an Interface, unlike Visual Basic .NET where a Class cannot be an Interface.

    Implementation
    inheritance enables the use of all functionality of an existing class, and the ability to extend that functionality without changing the code in the orginal class. Members are inherited from the base class but do not have to be implemented in the derived class.

    You can sort of fake implementation inheritance in Classic Visual Basic (just as you can fake multi-threading) but it's not native to the development environment.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, January 6, 2010 3:17 PM