Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-01-2004, 04:18 AM   PM User | #1
bennycs
New Coder

 
Join Date: Jun 2003
Location: USA
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
bennycs is an unknown quantity at this point
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.
bennycs is offline   Reply With Quote
Old 03-01-2004, 06:53 PM   PM User | #2
weronpc
Regular Coder

 
Join Date: Apr 2003
Location: Canada, Ontario, Mississauga
Posts: 312
Thanks: 0
Thanked 0 Times in 0 Posts
weronpc is an unknown quantity at this point
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.
weronpc is offline   Reply With Quote
Old 03-02-2004, 05:14 PM   PM User | #3
weronpc
Regular Coder

 
Join Date: Apr 2003
Location: Canada, Ontario, Mississauga
Posts: 312
Thanks: 0
Thanked 0 Times in 0 Posts
weronpc is an unknown quantity at this point
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
weronpc is offline   Reply With Quote
Old 03-05-2004, 10:05 PM   PM User | #4
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
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.
sad69 is offline   Reply With Quote
Old 03-05-2004, 11:01 PM   PM User | #5
weronpc
Regular Coder

 
Join Date: Apr 2003
Location: Canada, Ontario, Mississauga
Posts: 312
Thanks: 0
Thanked 0 Times in 0 Posts
weronpc is an unknown quantity at this point
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
weronpc is offline   Reply With Quote
Old 03-05-2004, 11:34 PM   PM User | #6
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 AM.


Advertisement
Log in to turn off these ads.