File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes What is adapter in Java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "What is adapter in Java ?" Watch "What is adapter in Java ?" New topic
Author

What is adapter in Java ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

What is adapter in Java ! i heard its old in java its not the new features but havn't got the chance to use it.

Some1 told me when we have lets see example:

if we have 3 methods in interface and but wants to implements only 1 methods then we can use this feature of java to chive this functionality.

I tried to find some good topics on this but couldn't success ed in net..

so if nay one have good knowledge of Adapter in java please share it.

and some code also i need to use it practically..


Thanks,
prabhat
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Prabhat,

in general an adapter is just a design pattern (I guess you're talking about these adapters). So in fact it's not only related to Java.

Usually an adapter is used like a real adapter for a water hose or something like that. That means you have some kind of class/interface/method signature you want to connect to with another class/interface/method but the interface of the client doesn't fit exactly to that of the service side (for example wrong type, method signature or something like that). Then you can create an adapter which translates between both interfaces so that you don't have do modify neither existing one.

Your example with partially implemented interface methods is a typical use case for adapters too. Swing or AWT make quite some use of it in order to provide convenient implementations (which often do simply nothing) for common interfaces. This way you can extend this class and only override the methods of the implemented interface as needed. Of course this only works if the interface does not require a useful implementation for all methods. You can just search the AWT or Swing API documentation for examples like the MouseAdapter class and many more adapters.

Btw. this is not related to Spring although Spring may make use of this pattern! Please choose the right forum more carefully if you expect to get an answer to you questions quickly

Marco
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Thanks and got the idea !

So if i would like to more then go for the Design Patterns Adapter..

Thanks a lot !

Regards,
prabhat
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Exactly! You'll find more than enough information about the adapter pattern or design patterns in general on the internet. Of course you will even find lots of concrete examples in many different programming languages etc.

Marco
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
yes correct !

Thanks
Vivek K Singh
Ranch Hand

Joined: Dec 22, 2009
Posts: 85

If you have 3 methods in an interface and want the concrete class to implement only one then you WILL have to make a abstract class and define the 2 non required methods in the abstract class and then create a concrete class that will extend this abstract class... (so you will be forced to provide impl for only one method)

Adapter design pattern simply makes 2 classes to talk to each other, which would not have been possible because of compatibility issues.


SCJP 6
Vivek K Singh
Ranch Hand

Joined: Dec 22, 2009
Posts: 85

If you have 3 methods in an interface and want the concrete class to implement only one then you WILL have to make a abstract class and define the 2 non required methods in the abstract class and then create a concrete class that will extend this abstract class... (so you will be forced to provide impl for only one method)

Adapter design pattern simply makes 2 classes to talk to each other, which would not have been possible because of compatibility issues.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Yes vivek you are right and i agree with you as what i know same !

If you don't want to implement any method of interface then you need to make the class as abstract which provides the facility to implement in the next child class.

But i know from some1 if you have let see....5 methods in interface and you want to implement only 1 out of 5 then with the help of ADAPTER we can achieve but how i am not clear..

If you know please share.

Interface parent {

int test();
void display();
int count();
void show();
void hide();
}

see above is interface and we are now here how helps us here...

class test implements parent {

int test() {
System.out.println("Hello we are in test ");
}

}

could you please let me know what change we can do and where ?

Regards,
Prabhat
Vivek K Singh
Ranch Hand

Joined: Dec 22, 2009
Posts: 85

Beats me, do post back here if you find about this adapter
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
An adapter is one of the classic design patterns. It is also a class in Java that implements an interface with a set of dummy methods. It lets you rapidly implement an interface. It also lets your class continue to work ever if the underlying interface acquires new methods. You can then subclass the adapter class and override just the methods you need. If you implemented the interface directly, you would have to write all the dummy methods yourself. Most commonly an adapter is used to help you rapidly construct your own Listener class to field events. By extending an adapter class, with KeyAdapter, FocusAdapter, WindowAdapter etc. you don’t have to write methods for events you are not interested in handling.
 
wood burning stoves
 
subject: What is adapter in Java ?