View Full Version : Can someone clear up Interfaces for me in Java
03-01-2004, 04:18 AM
I was a little confused on interfaces in Java....can anyone give a simple example of one, thank you.
03-01-2004, 06:53 PM
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.
03-02-2004, 05:14 PM
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.
03-05-2004, 10:05 PM
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,
03-05-2004, 11:01 PM
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
03-05-2004, 11:34 PM
Here is a tutorial discussing interfaces
This guy is a computer science teacher and has written many good tutorials on Java. Here is the rest of them
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.