Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2005, 05:32 AM
Authorized User
 
Join Date: Oct 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Polymorphism with Interfaces

I'm going through a VB.NET book at the moment and I've come to a part about Interfaces. The book defines an Interface as follows:

Publice Interface IDriveable
    Sub GoForward(ByVal Speed As Integer)
    Sub Halt()
    Function DistanceTraveled() as Integer()
End Interface

It then goes on to show a normal use with:

Implements IDriveable

However it then shows under the title of "Polymorphism with Interfaces" the following:

Public Sub GoSomewhere(ByVal As IDriveable)
' implementation omitted
End Sub

Anthis is where I get stuck how whould you implement an Interface when its pasted as a parameter. I tried defining it as:

Dim x as IDriveable
x.FuelLevel = 10

And that produced a runtime error of 'SystemNullReferenceException'. I then tried to define it as:

Dim x = new IDriveable

That gave a design time error of not being able to use new with an Interface.

Any ideas?

But that gives a runtime error
 
Old May 11th, 2005, 10:11 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can not create an instance of an Interface because it's an abstract concept.

The GoSomewhere method is not expecting an instance of an interface, but rather it is saying "Please pass me an instance of a class that implements IDrivable".

So if we have these classes:

Class Car
   Implements IDriveable
End Class

Class Boat
   Implements IDriveable
End Class

and this method:

Public Sub GoSomewhere(ByVal As IDriveable)
' implementation omitted
End Sub

We can do this:

Dim myTruck As New Car
Dim myShip As New Boat

GoSomewhere(myTruck)
GoSomewhere(myShip)

This works because Car and Boat each implement the interface that the method expects. Think of it like this: The abstract 'instance' of the interface is piggy-backed on top of the real instance of the class. This way you can do something that is related to the interface without caring what the class type is of the actual object instance.

A very practical example of this that you might not even be aware that you use all the time is IEnumerable. Whenever you use a ForEach, .NET is asking the object that you iterate thru for it's GetEnumerator method that is provided by the IEnumerable interface. ForEach doesn't care if it is working with an array or a collection as long as the type it's dealing with implement IEnumerable.

-Peter
 
Old May 11th, 2005, 12:50 PM
Authorized User
 
Join Date: Oct 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that excellent reply.





Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03:36 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 05:24 AM
interfaces malli_kv2 Java Basics 1 April 23rd, 2007 05:21 AM
What is Interface Polymorphism in C#? ramk_1978 C# 1 May 3rd, 2006 01:23 AM
[CH04] Interfaces Norm 2782 BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 3 June 16th, 2005 04:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.