Interface in VB.NET -TrickCode

An interface in VB.NET,Interfaces vs. Abstract Classes,VB.NET,VB.NET,interfaces
Share it:

Introduction

An interface in VB.NET is a collection of related members. An interface defines a specific kind of behavior that a given class or structure can implement. An interface is defined using the Interface keyword. The following are some of the properties of interfaces in VB.NET.

Interface in VB.NET

  1. An interface does not take any access modifier because all members within an interface are implicitly public and abstract by default.
  2. The members (methods, properties, events, and other type definitions) defined within an interface are defined using only their signatures. In other words, interface members can not have a body as the body is provided by any class or structure that wants to implement this specific kind of behavior defined by the interface.
  3. An interface can not define data fields only methods, properties, events, and type definitions are allowed.
  4. In VB.NET, the base class of any type is ultimately the System. Object class. But interfaces are exceptions to this. Interfaces do not specify any base class!
  5. Interfaces are useless (as they do not contain any code in the bodies of their members) until they are implemented by a class or structure.
  6. As a convention, all interfaces in VB.NET start with the 'I' prefix like IMyInterface.
  7. You can not create an object (New IMyInterface()) of an interface directly.
  8. An interface can inherit from another interface.
  9. If a class (or structure) implements an interface, IInterface1, and IInterface1 inherits from another interface, IInterface2, then the class must implement all the members of IInterface1 and IInterface2! This is equivalent to implementing both interfaces separately.
Public Interface IInterface1
    Sub Member1()
End Interface

Public Interface IInterface2
    Inherits IInterface1

    Sub Member2()
End Interface

Public Class MultipleInterfaceImplementation
    Implements IInterface2

    Public Sub Member1() Implements IInterface1.Member1
    End Sub

    Public Sub Member2() Implements IInterface2.Member2
    End Sub

End Class

An interface can be used like any other type when it comes to user interfaces as parameters, return types, variables, and so on. Let's create a simple interface and see how it defines a specific kind of behavior and how a class can implement that behavior.


Public Interface IDrawableShape
    Property Name()
    Sub Draw()
End Interface



The interface declaration above shows a simple interface called IDrawableShape that defines a single property Name and a single method Draw. As its name implies, the given interface expresses the behavior of any shape (circle, rectangle, square, etc) that can be drawn.

When shapes implement this interface, they are forced to provide an implementation for these two members. You, as the interface creator, do not know and need not know how they are implemented. But you know that if you call the Draw method, for instance, on any shape, the shape will be drawn in the correct form.

It's like you ask someone to do something and they do it. You are not interested in how that person does it, but as long as he does it, you are happy! When multiple shapes implement our interface, they are all considered to be of the same type and can, therefore, be looped through (for instance) as if they were a collection of exactly the same type! This is an advantage because VB.NET does not support multiple inheritances (having a class that inherits from multiple classes like in C++), but you can achieve that by implementing multiple interfaces each of which carries a specific kind of behavior.

So a class or struct in VB.NET can implement any number of interfaces (but can inherit from a single base class).

Let's see how we implement our interface.


Public Class Square
    'Code
End Class

Public Class Rectangle
    Inherits Square
    Implements IDrawableShape

    Private _name As String

    Public Property Name() As Object Implements IDrawableShape.Name
        Get
            Return _name
        End Get
        Set(ByVal value As Object)
            _name = value
        End Set
    End Property

    Public Sub Draw() Implements IDrawableShape.Draw
    End Sub

End Class



The code above shows a class Rectangle that inherits from a class Shape (the code for the Shape class is not shown because it's not really relevant here and is included only for completeness). Inheriting from the Shape class makes the Rectangle class shape and inherits any common functionality from it. Next, our Rectangle class implements the IDrawableShape interface and provides an implementation for the Name and Draw members. It's only after that we have actually implemented the IDrawableShape interface that it means anything! Note the Implements keyword in the Name property's and Draw method's signatures. This is known as the direct implementation of an interface.


Interfaces vs. Abstract Classes


You might be confused by VB.NET interfaces and abstract classes (as base classes) when you study them, but they are two different things. You can, for instance, implement as many interfaces as you want, but you can inherit from only one abstract base class. You can not have data fields in interfaces, but that's legal in abstract base classes. Interface members can not define bodies (implementation) were members of an abstract base might choose to have bodies. Interface members are only public as well as abstract (by default) and you can not change this. Abstract base class members, however, can be public, private, protected, and so on.
Share this article with your friends
Share it:

VB.NET

Post A Comment:

0 comments: