Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes adapter class !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adapter class !!" Watch "adapter class !!" New topic
Author

adapter class !!

salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
hi, question on adaoter class


WindowListener listner = new Terminator(); // how can i write this?
frame.addWindowListner(listner);
my problem is how can i write WindowListener listner = new Terminator();
look , WindowListener is an interface and it has no relation with WindowAdapter class. so, how can i write this way ? this is violating the objec creation rule.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There is a relationship: WindowAdapter implements WindowListener, which means Terminator does too. This is the whole point of these event-adapter classes: they implement the whole Listener interface with emoty implementations, allowing you to just override one method if you choose.


[Jess in Action][AskingGoodQuestions]
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
ok,
WindowAdapter implements WindowListener

as it is implementing that means i should define first the bodies of 7 methods of WindowListener,bcoz thats what implements means.see, i have not written bodies for those 7 methods of Windowlistner interface !!

---then i can override one method(of course it has defined)of WindowAdapter . right ?
but you see, i did not write bodies for those 7 methods for WindowAdapter class ifself.
so, how can i say, it is implementing ?

so, bottom line is as per your comments : as you are saying WindowAdapter implements WindowListener so i must write 7 method bodies first .
but i have not written that. how it works then ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

WindowAdaptor already implements the interface and provides implementations for each method. That means if you extend WindowAdaptor, your class automatically inherits implementations of all seven methods. You can choose to override one or more in your own subclass, but you're not required to implement any because they're already implemented.
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
WindowAdaptor already implements the interface and provides implementations for each method.

but how ? does those methods have empty braces only for their implementation. i am not quite sure.
something must be there like below...
class WindowAdapter implements WindowListner
{
method1 { } // is it like this way implemented ?
method2 { }

.......................
.......................
method7 { } // are these empty
}
are these implemented this way . if it is like this then i have no objection to extend this to class Terminator.
will you be explicit more ?
thanks
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Think about a MouseListener that responds to events generated by the mouse. It has 5 methods: clicked for receiving mouse clicks, entered for knowing when a mouse cursor enters the bounding box of a component, exited for when the cursor leaves that bounding box, pressed when a button is depressed, released when a button is released. (The click is a rapid sequence press-release cycle, different from an individual press or release.)
For most purposes, classes implementing this interface will only be interested in clicking. Some will want to respond to press and release, and almost none will want to implement the methods for enter and exit. But because these are all in the interface, they all must be implemented. And apps wishing only to implement the click functionality can and should implement empty functionality for the other methods. They're explicitly saying, I don't want to do anything when this event occurs.
The point of an "adapter" is to provide empty implementations to subclasses. Let's say I write a listener interface that has 20 methods. For my particular listener, it might be the case that most subclasses wish to do nothing at all for 19 of the 20 methods--they want empty implementations of those methods so the listener just ignores the event. So the solution is to provide an adapter, an abstract class that implements all of the methods of an interface with empty implementations:

Now, if you wish to implement the Foo interface but you are only interested in providing an implementation for bar():

...and you're done. In rare cases, you may want to use the adapter as it is, providing no implementation for any of the methods. This may happen if you're working with a system that forces you to hand in some kind of event listener but you're not interested in responding to any of the events. To do this, use an anonymous inner class:

When using adapters, you should never refer to classes polymorphically by their adapters. It would be wrong to write:

Rather, you should always prefer types based on the Foo interface rather than the FooAdapter. The reason is, what if someone comes along that does want to implement every method of the Foo interface? In that case, extending the FooAdapter buys them nothing, and it costs them the ability to extend a different class (remember, Java only allows you to extend one class, but you can implement many interfaces). Certainly, your array above should be able to accept Foos and FooAdapters alike. Since the contract is exactly the same on FooAdapter as Foo, there is never a good reason to polymorphically use FooAdapter instead of Foo.
(I wonder if you could declare the entire FooAdapter class protected to prevent its usage in place of Foo?)
sev
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by salvador rc:

are these implemented this way .

Sever's answer is absolutely correct, but I think I can answer in a slightly more succinct way:
"Yes".
Or to be a bit more verbose, yes, an empty pair of brackets is a perfectly good do-nothing implementation of a method that returns void.
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
hi, here is the same code but something different ..

its a annonymous class.
this will do the same thing as above.
but my unhappiness is due to see new WindowAdapter() in the code.
how the compiler will understand it has been extended to class Terminator(implicitly)
and all the methods are basically coming from WindowListner interface or implemented in the WindowAdapter class itself!!

as you know, i can not instantiate abstact WindowAdapter, bcoz its abstract.so, i have to extend this to a subclass for instantiate. so its really hazy to see new WindowAdapter() in the code. bcoz in the code i never have not explicitly subclassed to Terminator class. why the compiler do not complain to see instantiating a abstract class in the above code like that way ?
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
sever oon has given an example that way. do i have to take it as a grammer of the language?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

That's just how it works.
new AnyClass() { ... }
means "create an instnce of an anonymous class that extends AnyClass and adds the following features..."
Note that in your later example, "Terminator" has nothing to do with it; the anonymous class extends WindowAdapter directly.
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
new AnyClass() { ... }
means "create an instnce of an anonymous class that extends AnyClass and adds the following features..."

----thanks Ernest , this statement is very much important to me.

Note that in your later example, "Terminator" has nothing to do with it; the anonymous class extends WindowAdapter directly.
------you are rirgt.
it removed my confusion.
thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adapter class !!