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.
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
Joined: May 22, 2010
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...
@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..!
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..
author and iconoclast
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.
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...
Joined: May 22, 2010
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...!
author and iconoclast