Results 1 to 10 of 10

Thread: interfaces

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    interfaces

    hmmm...what is the real use of them? they look very similar to classes

  2. #2

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm...they:

    have no access modifiers...
    cant contain code bodies
    cant define field members
    type defenition members are forbidden

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah i see...interfaces are like bones so i can inherit from them and has some built-in options?

    edit: yea..looks like they are bones so then one can see what to has to make in a class..
    Last edited by PT Exorcist; Nov 5th, 2002 at 05:57 PM.

  4. #4
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    They are just a contract. They don't have any implementation. This means if you inherit from an interface, you will have to implement all the methods and properties of the interface.

    But, what this buys you is that you can treat the same any object that inherits a particular interface.
    -scott
    he he he

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I'm having a really hard time coming to terms with interfaces. It just does'nt make any sense to implement an interface then writing all the code for the methods and properties it exposes. Why not just write your own methods or class? I guess I just need to see a real world example.
    Dont gain the world and lose your soul

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Image you're given a task to create an object model, 'Vehicle'. Two common actions present in all vehicle types would be 'TurnLeft' and 'TurnRight'. This is something that you want all objects of type 'Vehicle' to implement. Rather than trying to come up with a generic method and let all subclasses override, you could just set up an interface that contractually binds all subclasses to adhere to. I hope that helped a little..

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Ok, I think I got it, I'll go read my text now
    Dont gain the world and lose your soul

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also think of it like this: You know how in .NEt a lot of the objects have the same method (i.e. Clone, Contains). Objects that can work as enumerable (can be used in a For...Each) implement the IEnumerable which has a GetEnumrator method that it must implement in order to work with For..Each. Same with IClonable which has a Clone method that must be implemented. Then when You go to run a For..each or Clone the Framework queries for one of these interfaces then uses that part to do the expected method. Its almost like getting a derived class because you can do things like this:

    VB Code:
    1. Public Class MyObject1
    2.   Implements ICloneable
    3.  
    4.   Public Function Clone As Object Implements ICloneable.Clone
    5.     'blah blah
    6.   End Function
    7. End Class
    8.  
    9. Public Class MyObject2
    10.   Implements ICloneable
    11.  
    12.   Public OtherProperty As String
    13.  
    14.   Public Function Clone As Object Implements ICloneable.Clone
    15.     'blah blah
    16.   End Function
    17. End Class
    18.  
    19. Dim MyObject1Instance As New MyObject1()
    20. Dim MyObject2Instance As New MyObject2()
    21. Dim ic As ICloneable=MyObject2Instance
    22. Dim obj As Object=ic.Clone()
    23. ic=MyObject1Instance
    24. Dim obj2 As Object=ic.Clone()
    25. 'regardless of the object type if it supports the interface you can convert it to the interface object and use it as such.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's called polymorphism. Using this mechanism you can treat objects like the interfaces you derive from.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width