Re: classes and interfaces

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mar 24, 11:35 am, Inictus <Stormgu...@xxxxxxxxxxx> wrote:
On Mar 24, 6:53 am, "John" <johndev...@xxxxxxxxxxx> wrote:



Hi,

I've wrote a small class called Car and attached an interface iCar. So far,
so good....

What i would like to know is ... What is the correct use for the
interface...?

sub Drive(myCar as iCar){

}

... Works great....

However...

Dim myCar as new iCar

doesn't work....

Kind regards

John

As Seth demonstrated, you still instantiate your object the normal way
- i.e. using the constructor of the Car class.

Just remember that an interface is a contract of sorts. If you
implement an interface on a class, you are guarenteeing that this
class implements the properties and methods of the interface. You
cannot instantiate an object from an interface directly.

So, when your car class implements the iCar interface, you are saying
that your car class implements those properties and methods defined on
the iCar interface. The Car instance (myCar in your example) is
considered to be of type iCar (as well as type Car, as well as type
object and as well as any other classes that you have inherited from).
So you can pass the instance of myCar to a sub that takes parameters
of type: Car, iCar and Object (or if you have inherited from another
class).

In the sub Drive you have defined, remember that when you pass your
myCar instance to it, you will only have access to the properties and
methods defined on the iCar interface (because sub Drive takes an
object of type iCar). Any other properties and methods you have
defined on the Car class beyond those specified on the iCar interface
you will not have access to (unless you do a type conversion - but
then you risk the possibility of an exception being thrown if you pass
an object not of the type you are converting to).

For people reading this thread and wondering what the benefits of
using an interface is, then here you go.

Suppose you want to be able to handle multiple forms of automobiles,
and all of them need to have certain properties and methods (number of
passengers, drive, etc) but each have different implemantions. You do
not want the ability to tell them apart, as you might not know the
exact classes, say if you load the types from an external dll at
runtime. With an interface you can force those properties and methods
and be able to handle any type at runtime, regardless of "true" type.

For example.

//////////
Public Interface IAutomobile
Property Passengers As Integer
Sub Drive()
End Interface
//////////

You can use that interface in a generic method to do anything to any
automobile.

////////////
Public Sub DriveAnAutomobile(automobile As IAutomobile)
If automobile.Passengers > 0 Then
automobile.Drive()
End If
End Sub
////////////

This comes in handy as you can have multiple types of automobiles use
that method, regardless of whether the assembly with the
'DriveAnAutomobile' knows about the specific types.

////////////
Public Sub Main()
Dim car As IAutomobile = New Car()
Dim truck As New Truck() '// Assume Truck implements IAutomobile

DriveAnAutomobile(car)
DriveAnAutomobile(truck)
End Sub
////////////

Thanks,

Seth Rowe [MVP]
.



Relevant Pages

  • Re: implementing roles in OOP......
    ... > "An application implements a system of responsibilities. ... > single purpose) within a given level of abstraction. ... Note that since an interface defines the messages an object will ... Car not ComponentList). ...
    (comp.object)
  • Re: inheritance
    ... possible to have an instance of a Car class. ... An abstract class is a lot like an interface, except that it may provide at least some basic implementation useful for classes that will inherit it. ... And so in the same way that interfaces are all about polymorphism, that's what makes abstract classes so interesting. ... So, for the purposes of conceptualizing the way abstract classes work, I would say you should think about how you conceptualize interfaces. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: classes and interfaces
    ... I've wrote a small class called Car and attached an interface iCar. ... The Car instance (myCar in your example) is ...
    (microsoft.public.dotnet.languages.vb)
  • Re: on the strange weakness of Graphical User Interface Languages
    ... most drivers don't care how their car works, as long as it runs, most ... I am happy that I don't have to use reins and spurs to drive a car. ... At recent user interface conferences, ... had to appeal to the mythical average user. ...
    (comp.lang.lisp)
  • Re: mysterious compile error, method which exists isnt found
    ... I had another .aspx webpage in the same folder with the name ... Car, so I solved it by referencing the Car class by me.carspace.BLL.Car ...
    (microsoft.public.dotnet.framework.aspnet)