aspose file tools*
The moose likes Beginning Java and the fly likes What exactly the use of an interface other than readability of code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What exactly the use of an interface other than readability of code?" Watch "What exactly the use of an interface other than readability of code?" New topic
Author

What exactly the use of an interface other than readability of code?

Cj Jwalan
Greenhorn

Joined: May 22, 2010
Posts: 4
Seriously I still don't understand what this interfaces do in particular...

Or.. can I write an interface exactly like to ActionListener interface (say ActionListenerExt ) and use it the same way as ActionListener..

As far as i know, I can just write an interface with some fn signatures and some constants...! NO Big Use..???!

Please somebody make it clear for me..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Gibberish is not cool. You can change your display name here. Thanks!

On to your question: yes, absolutely, you can. There's nothing magical about ActionListener (for example.) But what's going on is that inside the code for (for example) JButton, there is code that will call actionPerformed() on any ActionListener instances that have been registered with the button. The button's code can just call this actionPerformed() method without knowing anything at all about your class -- the one you write yourself that implements ActionListener. Your class didn't even exist when the button code was written and compiled, and that's what's cool about it. An interface provides a way for different, unrelated modules to communicate and collaborate.


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not just about readability (in fact, I don't think that's really the issue)--it's about *pluggability*.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

think of a java interface like a USB port. You can plug all kinds of different things in, and it will work.

Another example...You and I are both competing for a promotion. We are both going to write code that connects and talks to some third party piece of hardware.

The vendor says "We have a class called 'talksToIt', which implements the 'talker' interface."

You write your code to use talksToIt objects, calling it's methods directly, etc.

I write my code to use the 'talker' interface, using the methods defined in there.

Then, the night before we're supposed to present our stuff to the board, the vendor says "We have a NEW class, called talksToItBetter, which still implements the 'talker' interface, but it doesn't have nearly as many bugs, and it runs ten times faster!!!".

What happens? You have to re-write all your code to now use talksToItBetter objects, use those methods, which may have different signiatures, return values, etc. The original class uses an array, which you were directly accessing, and now you don't know what it's doing...

I, on the other hand, can simply drop their new code in, and I don't have to change my code AT ALL.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Cj Jwalan
Greenhorn

Joined: May 22, 2010
Posts: 4
@Ernest Friedman-Hill

Sorry.. but thats my name. CJ Jwalan . "jxw" is my internet name... any way I changed it..

And Thanks for your help.. I understand it to some extent.. bot not fully..

But how exactly the addActionLisetener works with this interface only...?! I mean how that link is made...?!

To be more precise, take the Runnable interface. It just has a function signature run(). That's all about it (Please , correct me if a I am wrong..).
Can I create a thread enabled class just writing a function name "run()" (with body , of course ) in it, without implementing "Runnable" interface....?
I think we can't. .. if its so , Why...?! How the Threading in java related to "run()" function in the "Runnable" interface...

Please clarify..

@Ernest Friedman-Hill & @fred rosenberger

Thanks to both of you. Both of your explanation gave me some new thoughts.
I want to explain more about my confusions but am not good in english..!

@fred rosenberger

Can you please elaborate your example, like you give some values..? I didn't get it fully...

interface "talker" just has some function signature in it right...?!
Actually we are writing function body in our class.. so how this code will be suitable to new changes...?!

I am confused... Please Clarify..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

JButton (at least conceptually) contains code like this:



Now, you can write a class like this:



... and then you can do this:



... and then when the button is pressed, the message is printed, because that loop calls your actionPerformed() method.

So you see there's nothing at all special about that interface -- it's just that the JButton code uses it. Runnable is just the same: the Thread class holds a reference to a Runnable object, and the Thread calls run() when it is started. The Runnable interface is not magic in any way, it's just that Thread is written to use it.

You can use this same pattern in your own applications: any time there's a task that could be implemented in many different ways, you can define a interface and then write a class like JButton or Thread that works with user-written objects that implement that interface.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Look at something like the List interface. It defines a bunch of methods that let you (among other things) add an object to the list, get an object at a specific position, or check to see if the list is empty.

There are LOTS of ways to write code that acts like a list. It can be done as a linked-list, an array, a hash...or many, many other ways.

But all of them would need to have a way to add an object, get an object at a specific position, or check to see if the list is empty or not.

However, each of them would do those three things in a slightly different way. A linked list would check to see if the root element pointer actually pointed to something, an array you'd have to check each spot in the array to see that all were empty, etc.

If I am implementing a List, I probably DON'T CARE how it's done. All I want to know is "is this list empty - yes or no?".

By using the interface, I don't NEED to know. I just call "isEmpty()", and it does it. If I decide that an ArrayList would work better than a LinkedList, I can use it. Then, if later I decide the LinkedList would have been better, I can change it in ONE SPOT, and I'm done, because the methods I call don't change...

Cj Jwalan
Greenhorn

Joined: May 22, 2010
Posts: 4
@Ernest Friedman-Hill

Wow..! Wow..! exactly thats what I was looking for actually...!

I just got a lot of things now...! your explanations changed my big confusion which lasted almost 1 year or so..
I was not working in java officially, other than some simple freelance web applications as well as some student projects..
I always had an ambition towards java... yet I didn't take the interface thingy never in my java programs (other than the java implemented ones) ...

So the instance of the class which implements the interface can be considered as the instance of the interface too ( of which I never thought of..!).. Am I Right Now..??!
This is how it is defined to use in specific targets... This is how that "magical" link is made, on which my confusion had grown...

"the Thread class holds a reference to a Runnable object, and the Thread calls run() when it is started."

A lot of Thanks to you...!

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Cj Jwalan wrote:
So the instance of the class which implements the interface can be considered as the instance of the interface too ( of which I never thought of..!).. Am I Right Now..??!


Exactly right.
Cj Jwalan
Greenhorn

Joined: May 22, 2010
Posts: 4
@fred rosenberger

Many thanks to you too..

with the light of Mr.Ernest Friedman-Hill's explanations I understood your examples now...

Thanks I will be back with more doubts... Now I seriously feel a confidence to swim in the java pool...

Thanks..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What exactly the use of an interface other than readability of code?