Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2003
    Location
    USA
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can someone clear up Interfaces for me in Java

    I was a little confused on interfaces in Java....can anyone give a simple example of one, thank you.

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In Java, most of the Event Handler (control events, such as clicking the button, moving mouse)

    When you use an interface class, you must include all the methods that are in the interface class, if the interface class has 3 mothods, you must have all the 3 methods in your code (over ride 3 methods).

    I have to go home now, I write more later.

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bennycs

    Just remember, if the interface contains n# of methods, you must use all the methods. if you don't need to use the methods, just make it empty.

    Interface use impletments, not extends.

    bennycs, let me know if you have more questions. I will do my best to explain where you don't understand.

    Mike

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think a good example/definition of an Interface goes something like this:

    Think of an audio player. It has some functions: stop, pause, play, rewind, forward, eject

    This is an interface. The name of the interface can be AudioPlayer, and the functions would be the methods.

    Now if you were to create a class, such as TapePlayer or CDPlayer, you would implement those functions. And you have to implement ALL of those functions.

    The implementation in each class will be different (playing a tape is different from playing a cd...).

    However, you can also have more methods in the classes that implement the interface if you'd like. For example, in CDPlayer, you might have nextTrack and prevTrack as these are methods unique to the CDPlayer class (TapePlayer doesn't have this functionality...).

    When you see the different tape/cd players out there on the market, you'll notice they each look different, but their core functionality is the same. That is, they all implement the AudioPlayer interface, but they've implemented each method differently, and have their own characteristics and methods (different colors, lights, button layouts, etc...).

    A class can also implement more than one interface (whereas it can only extend one class). For example, if the interface Calculator existed, then you could create a class that was an AudioPlayer and a Calculator at the same time.

    Interfaces are important because they hide the implementation and allow users to run the methods that they know of. When you press play on a CDPlayer or a TapePlayer you know it's going to play, and not do something else (at least you hope not..), and you don't care how it plays it. But that interface defines that object will have the play feature, and that the object won't call it 'go' or something else.

    If a TapePlayer were installed in two different cars, one with an automatic antenna, and one with a regular antenna, should the TapePlayer have to recognize between them? No. The Antenna interface tells us that we merely have to ask for a radio station (94.5 FM for example), and we will receive that audio and can then output the audio to the speakers. The AutomaticAntenna class will take care of the implementation of bringing the antenna up or down, or whatever.

    I think that should make sense and explain what interfaces are, why they are used, and why they are important.

    If I missed anything out, or was incorrect anywhere, please correct me. I'm sure there's more to it, it just depends how much detail you want.

    Hope that helps,
    Sadiq.

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sad69 done a great job explainning what is Interface.

    2 important rules,
    you implements interface and must over ride (use) all the methods that are in the interface.

    Once again, good job Sad69

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Here is a tutorial discussing interfaces

    http://www.developer.com/tech/article.php/999971


    This guy is a computer science teacher and has written many good tutorials on Java. Here is the rest of them

    http://www.dickbaldwin.com/toc.htm
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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