1. Computing

Multiple Interfaces

Does "multiple interfaces" make VB.NET totally Object Oriented?

From , former About.com Guide

According to C++ programmers, a lack of something called "multiple inheritance" in VB.NET (and other .NET languages - we're certainly not alone in this) make it slightly less than fully object oriented. The rap is that you need the ability of a class to inherit from more than one parent to be able to do dynamic binding and polymorphism. It's not VB.NET, but to be very brief about it, if VB.NET were able to do multiple inheritance, then syntax like this would be possible:


' Not correct code!
Class derivedClass
    Inherits baseClass1
    Inherits baseClass2

If this was possible, derivedClass would have all of the methods and properties of both baseClass1 and baseClass2. This isn't a political site, but to get the idea across, lets say that we want to create a derived class called "person" which inherits from two base classes: "human_being" and "corporation". Then a "person" object could issue shares, operate simultaneously around the world, and live forever. At the same time, a "person" object would have civil rights and could sue another person object in court. That's multiple inheritance.

If you're restricted to multiple interfaces, then you would have to define a human_person and a corporate_person separately. It would not be possible to have a single object with the characteristics of both at the same time.

The point of an Interface is that it defines the things that must be implemented in an object, but it leaves open the way it's done. In other words, an Interface defines the what but not the how. A basic introduction to interfaces can be found here: Interface Definitions and Why You Should Care. You can read an article here at About Visual Basic where a reader's questions about defining and using an interface are answered. (Using that traditional example, a dog as an instance of a mammal.) Using Interfaces In Separate Files. The basics of interfaces are explained in more detail in that article, so I won't go into them in depth here.

Interfaces are used a great deal in .NET itself. An article explaining just one of those uses in .NET can be found here: IEnumerable and IEnumerator Explained in Language You Understand. Another popular interface is the ICustomFormatter interface in .NET. This article explains it. Formatting Strings, Numbers, and Other Objects.

To define separate interfaces for a corporation and a human, you can start by defining the interfaces themselves:


Public Module LegalEntities
    Public Interface ICorporation
        Property theName As String
        Property Shareholders(ByVal index As Integer) As String
        Sub DoBusiness()
        Event FiscalYearEnd(ByVal theYear As Date)
        Sub PayTaxes(ByVal Income As Double)
    End Interface
    Public Interface IHuman
        Property theName As String
        Property CitizenOf As String
        Sub Vote()
        Event DieEventually(ByVal theTOD As Date)
        Sub PayTaxes(ByVal Income As Single)
    End Interface
End Module

Notice that the interface names both start with a capital "I". That's not required, but it's very traditional. Don't break the chain!

The two interfaces defined here show the elements (properties, subs, functions, events) that will be required when a class implements the interface. So, you can see that both humans and corporations have names and pay taxes (well ... most of them do) but only humans vote and eventually die. (Corporations are like vampires; they only die if you drive a stake into their heart. Joke, fercryinoutloud!) The default declaration for interfaces is Friend (the scope is the assembly) but they're often declared as Public for use in libraries. In the example below, I have placed all of the interface definition in a module named LegalEntities for simplicity and that is a popular choice.

To use the interface, you have to implement it in a class ...


Class human_person
    Implements IHuman

Visual Studio autocomplete gives you a lot help! As soon as you press enter, autocomplete enters a shell of code that contains the basic code blocks for all of the elements that have to be coded. The class itself implements the interface and each element implements a corresponding required element in the interface definition.

--------
Click Here to display the illustration
--------

To keep this Quick Tip quick, I've entered very basic code to complete the definition. Here's the human_person code:


Class human_person
    Implements IHuman
    Private _CitizenOf As String = "USA"
    Public Property CitizenOf As String _
        Implements LegalEntities.IHuman.CitizenOf
        Get
            Return _CitizenOf
        End Get
        Set(value As String)
            _CitizenOf = value
        End Set
    End Property
    Public Event DieEventually(theTOD As Date) _
        Implements LegalEntities.IHuman.DieEventually
    Private _Name As String
    Public Property theName As String _
        Implements LegalEntities.IHuman.theName
        Get
            Return _Name
        End Get
        Set(value As String)
            _Name = value
        End Set
    End Property
    Public Sub PayTaxes(Income As Single) _
        Implements LegalEntities.IHuman.PayTaxes
        Console.WriteLine("Paying Taxes!!")
    End Sub
    Public Sub Vote() Implements _
        LegalEntities.IHuman.Vote
        Console.WriteLine("Voting!!")
    End Sub
End Class

The final step is to use the Interface, as implemented in a specific class including all the unique code for this application. Here's an example for the IHuman interface:


Dim human_obj As New human_person
human_obj.theName = "Joe Average"
human_obj.Vote()
human_obj.PayTaxes(0.01)

Notice that when a different object is defined for a corporate person, a method for voting isn't present.

--------
Click Here to display the illustration
--------

  1. About.com
  2. Computing
  3. Visual Basic
  4. Quick Tips
  5. Multiple Interfaces in Visual Basic .NET

©2013 About.com. All rights reserved.