<!doctypehtml><html lang=en><title>
   Adapter Design Pattern - Javapapers
  </title><meta content="An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need. In real world the […]"name=description><meta charset=utf-8><meta content="text/html; charset=utf-8"http-equiv=Content-Type><meta content=width=device-width,initial-scale=1 name=viewport><link title="RSS 2.0"href=https://javapapers.com/feed/ rel=alternate type=application/rss+xml><link href=/apple-touch-icon.png?v=261118 rel=apple-touch-icon sizes=180x180><link href=/favicon-32x32.png?v=261118 rel=icon sizes=32x32 type=image/png><link href=/favicon-16x16.png?v=261118 rel=icon sizes=16x16 type=image/png><link href=/site.webmanifest?v=261118 rel=manifest><link color=#5bbad5 href=/safari-pinned-tab.svg?v=261118 rel=mask-icon><link rel="shortcut icon"href=/favicon.ico?v=261118><meta content=#da532c name=msapplication-TileColor><meta content=#ffffff name=theme-color><meta content="Adapter Design Pattern - Javapapers"property=og:title><meta content=Javapapers property=og:site_name><meta content="An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need. In real world the […]"property=og:description><meta content=https://javapapers.com/design-patterns/adapter-pattern/ property=og:url><meta content="Adapter Design Pattern - Javapapers"property=twitter:title><meta content=@javapapers name=twitter:site><meta content=@javapapers property=twitter:creator><meta content=article property=og:type><meta content=https://javapapers.com/wp-content/uploads/2011/05/Adapter.jpg property=og:image><meta content=summary_large_image property=twitter:card><meta content="An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need. In real world the […]"property=twitter:description><meta content=https://javapapers.com/wp-content/uploads/2011/05/Adapter.jpg property=twitter:image><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-4275351-1', 'auto');
  ga('send', 'pageview');</script><meta content=on http-equiv=x-dns-prefetch-control><link href=//www.google-analytics.com rel=dns-prefetch><link href=//www.youtube.com rel=dns-prefetch><link href=//googleads.g.doubleclick.net rel=dns-prefetch><link href=//static.doubleclick.net rel=dns-prefetch><link href=//www.google.com rel=dns-prefetch><link href=//i.ytimg.com rel=dns-prefetch><link href=//yt3.ggpht.com rel=dns-prefetch><link href=//fonts.gstatic.com rel=dns-prefetch><link href=https://javapapers.com/wp-includes/css/dist/block-library/style.min.css?ver=5.5.6 id=wp-block-library-css media=all rel=stylesheet><link href=https://javapapers.com/wp-content/themes/papers/papers_17191.css id=main-style-css media=all rel=stylesheet><link href=https://javapapers.com/design-patterns/adapter-pattern/ rel=canonical><body onload=prettyPrint()><div id=site_header><div class=content><a title="Android & Java Blog"href=/ id=java-blog> <img alt="Android & Java Blog"height=29 src=https://javapapers.com/wp-content/themes/papers/images/javapapers.png width=142> </a><span class=since> Since 2008 </span></div></div><div class=content><div id=tutorial-body><h1>Adapter Design Pattern</h1><div class=modified-on>Last modified on October 5th, 2014 by Joe.</div><p>An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Adapter design pattern is used when you want two different classes with incompatible interfaces to work together. The name says it all. Interfaces may be incompatible but the inner functionality should suit the need.<p>In real world the easy and simple example that comes to mind for an adapter is the travel power adapter. American socket and plug are different from British. Their <a title="Java interface"href=https://javapapers.com/core-java/abstract-and-interface-core-java-2/java-interface/> interface </a> are not compatible with one another. British plugs are cylindrical and American plugs are recangularish. You can use an adapter in between to fit an American (rectangular) plug in British (cylindrical) socket assuming voltage requirements are met with.<p><img class="alignright size-full wp-image-524"alt height=239 loading=lazy src=https://javapapers.com/wp-content/uploads/2011/05/Adapter.jpg title=Adapter width=297><h2>How to implement adapter design pattern?</h2><p>Adapter <a title="Introduction To Design Patterns"href=https://javapapers.com/design-patterns/introduction-to-design-patterns/> design pattern </a> can be implemented in two ways. One using the inheritance method and second using the <a href=https://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/> composition </a> method. Just the implementation methodology is different but the purpose and solution is same.<h3>Adapter implementation using inheritance</h3><p>When a class with incompatible method needs to be used with another class you can use inheritance to create an adapter class. The adapter class which is inherited will have new compatible methods. Using those new methods from the adapter the core function of the base class will be accessed. This is called “ <a title="IS-A Relationship"href=https://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/> is-a </a> ” relationship. The same real world example is implemented using java as below. Dont worry too much about logic, following example source code attempts to explain adapter design pattern and the goal is simplicity.<pre class="prettyprint lang-java">public class CylindricalSocket {
	public String supply(String cylinStem1, String cylinStem1) {
		System.out.println("Power power power...");
	}
}

public class RectangularAdapter extends CylindricalSocket {
	public String adapt(String rectaStem1, Sting rectaStem2) {
		//some conversion logic
		String cylinStem1 = rectaStem1;
		String cylinStem2 = rectaStem2;
		return supply(cylinStem1, cylinStem2);
	}
}

public class RectangularPlug {
	private String rectaStem1;
	private String rectaStem2;
	public getPower() {
		RectangulrAdapter adapter = new RectangulrAdapter();
		String power = adapter.adapt(rectaStem1, rectaStem2);
		System.out.println(power);
	}
}
</pre><h3>Adapter implementation using composition</h3><p>The above implementation can also be done using composition. Instead of inheriting the base class create adapter by having the base class as attribute inside the adapter. You can access all the methods by having it as an attribute. This is nothing but “ <a title="HAS-A Relationship"href=https://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/> has-a </a> ” relationship. Following example illustrates this approach. Difference is only in the adapter class and other two classes are same. In most scenarios, prefer composition over inheritance. Using composition you can change the behaviour of class easily if needed. It enables the usage of tools like dependency injection.<pre class="prettyprint lang-java">public class CylindricalSocket {
	public String supply(String cylinStem1, String cylinStem1) {
		System.out.println("Power power power...");
	}
}

public class RectangularAdapter {
	private CylindricalSocket socket;

	public String adapt(String rectaStem1, Sting rectaStem2) {
		//some conversion logic
		socket = new CylindricalSocket();
		String cylinStem1 = rectaStem1;
		String cylinStem2 = rectaStem2;
		return socket.supply(cylinStem1, cylinStem2);
	}
}

public class RectangularPlug {
	private String rectaStem1;
	private String rectaStem2;
	public getPower() {
		RectangulrAdapter adapter = new RectangulrAdapter();
		String power = adapter.adapt(rectaStem1, rectaStem2);
		System.out.println(power);
	}
}
</pre><h2>Adapter design pattern in java API</h2><p>java.io.InputStreamReader(InputStream) <br> java.io.OutputStreamWriter(OutputStream)<div class=share><ul><li><a href=https://www.facebook.com/sharer/sharer.php?u=https://javapapers.com/design-patterns/adapter-pattern/> <img title="Share on Facebook"src=https://javapapers.com/wp-content/themes/papers/images/facebook-social.png> </a><li><a href="https://twitter.com/intent/tweet?original_referer=https://javapapers.com&source=tweetbutton&text=Adapter Design Pattern;url=https://javapapers.com/design-patterns/adapter-pattern/&via=javapapers"> <img title="Share on Twitter"src=https://javapapers.com/wp-content/themes/papers/images/twitter-social.png> </a><li><a href="https://www.linkedin.com/shareArticle?mini=true&url=https://javapapers.com/design-patterns/adapter-pattern/&media=Adapter Design Pattern"> <img title="Share on Linkedin"src=https://javapapers.com/wp-content/themes/papers/images/linkedin-social.png> </a></ul></div><div class=related-articles><h2>Popular Articles</h2><ul class=related-articles-list><li><a href=https://javapapers.com/design-patterns/decorator-pattern/> Decorator Design Pattern </a><li><a href=https://javapapers.com/design-patterns/facade-design-pattern/> Facade Design Pattern </a><li><a href=https://javapapers.com/design-patterns/bridge-design-pattern/> Bridge Design Pattern </a></ul></div><div class=comments-area id=comments><h2 class=comments-title>Comments on "Adapter Design Pattern"</h2><ol class=commentlist><li class="comment even thread-even depth-1"id=comment-5388><div class=comment-body id=div-comment-5388><div class="comment-author vcard"><cite class=fn> mahendra </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-5388> 19/05/2011 at 10:28 am </a></div><p>could you add the UML Diagram for adapter design pattern as well.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-5479><div class=comment-body id=div-comment-5479><div class="comment-author vcard"><cite class=fn> Maniganda Prakash </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-5479> 22/05/2011 at 4:13 am </a></div><p>Correct the spelling mistake of recangularish.</div><li class="comment even thread-even depth-1"id=comment-5501><div class=comment-body id=div-comment-5501><div class="comment-author vcard"><cite class=fn> Dev Ghotkule </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-5501> 23/05/2011 at 1:47 pm </a></div><p>Why we prefer composition over inheritance?</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-5668><div class=comment-body id=div-comment-5668><div class="comment-author vcard"><cite class=fn> Code Explosion </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-5668> 29/05/2011 at 4:03 pm </a></div><p>Good work!<p>can you please implement rest of the patterns. You tutorial looks helpful.<p>Thanks.</div><li class="comment even thread-even depth-1"id=comment-5704><div class=comment-body id=div-comment-5704><div class="comment-author vcard"><cite class=fn> Debasis Samantaray </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-5704> 31/05/2011 at 9:48 am </a></div><p>I am sorry to say. <br> The information here is not complete.<p>The examples are suitable but complex.There is no description about who adapt whome ,and other detail.<p>This pattern can be explained easily with small example.The readers expect to understand the entities and there relationship.So that reader can implement in other case easily.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-6246><div class=comment-body id=div-comment-6246><div class="comment-author vcard"><cite class=fn> Narendra </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-6246> 30/06/2011 at 9:22 am </a></div><p>Please can you tell Advantages & Disadvantages</div><li class="comment even thread-even depth-1"id=comment-7437><div class=comment-body id=div-comment-7437><div class="comment-author vcard"><cite class=fn> speed </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-7437> 12/09/2011 at 7:42 am </a></div><p>does the RectangularAdapter class have to include CylindricalSocket object as a member variable?<p>If the adapt() method in the adapter class were to use a temporary variable of CylindricalSocket class – the logic (ofcourse) would still work. Question is whether it would still be called an Adapter pattern using Composition?<p>Thanks.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-7625><div class=comment-body id=div-comment-7625><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-7625> 29/09/2011 at 2:19 am </a></div><p>Thank you very much for writing the post. I’m having one question here and I hope to hear your teaching. I’m wondering is there any harm to rework the code and doing it without the adapter pattern, i.e.:<p>public class Socket{ <br> public String supply(String s1, String s){ <br> return “wehlaetla”; <br> } <br> }<p>public class Plug{ <br> Socket sock = new Socket(); <br> public void getPower(){ <br> sock.supply(s1, s2); <br> } <br> }</div><li class="comment even thread-even depth-1"id=comment-8535><div class=comment-body id=div-comment-8535><div class="comment-author vcard"><cite class=fn> Parmod </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-8535> 06/12/2011 at 5:24 pm </a></div><p>HI Sir <br> You have given nice example. <br> I am really fan you</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-8720><div class=comment-body id=div-comment-8720><div class="comment-author vcard"><cite class=fn> Ahmad </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-8720> 19/12/2011 at 12:42 pm </a></div><p>very nice example and now I am clear about this pattern.<p>@anonymous your code:<p>public class Socket{ <br> public String supply(String s1, String s){ <br> return “wehlaetla”; <br> } <br> }<p>public class Plug{ <br> Socket sock = new Socket(); <br> public void getPower(){ <br> sock.supply(s1, s2); <br> } <br> }<p>is not an example of adaptation here you are putting rectangular socket in rectangular plug. :)</div><li class="comment even thread-even depth-1"id=comment-9020><div class=comment-body id=div-comment-9020><div class="comment-author vcard"><cite class=fn> Anantha Prakash N </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-9020> 04/01/2012 at 1:25 pm </a></div><p>Hi Very Nice Post i simply enjoyed reading but i am unable to understand the program. <br> I think there will be a Implementer class which i could not find. <br> Can you please help me in understanding the program or can you please send me a full program it will be really helpful as i have a client interview next Monday..</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-9626><div class=comment-body id=div-comment-9626><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-9626> 30/01/2012 at 3:46 pm </a></div><p>very clean explaination</div><li class="comment even thread-even depth-1"id=comment-9627><div class=comment-body id=div-comment-9627><div class="comment-author vcard"><cite class=fn> dinesh bansal </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-9627> 30/01/2012 at 3:46 pm </a></div><p>very clean explaination</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-9701><div class=comment-body id=div-comment-9701><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-9701> 02/02/2012 at 5:38 pm </a></div><p>Thanks for explanation could you please give bit clear example with implementation.</div><li class="pingback even thread-even depth-1"id=comment-9768><div class=comment-body id=div-comment-9768><div class="comment-author vcard"><cite class=fn> <a rel="external nofollow ugc"class=url href=https://javapapers.com/design-patterns/bridge-design-pattern/> Bridge Design Pattern </a> </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-9768> 05/02/2012 at 8:55 pm </a></div><p>[…] adapter design pattern helps it two incompatible classes to work together. But, bridge design pattern decouples the […]</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-10160><div class=comment-body id=div-comment-10160><div class="comment-author vcard"><cite class=fn> mukesh </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-10160> 21/02/2012 at 1:34 pm </a></div><p>nice one</div><li class="comment even thread-even depth-1"id=comment-10974><div class=comment-body id=div-comment-10974><div class="comment-author vcard"><cite class=fn> Harish Mangalore </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-10974> 29/03/2012 at 10:30 pm </a></div><p>Good one!!!!<p>It would be better if you provide before and after design diagramms the way you have provided for Bridge patter.<p>Keep writing….</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-11828><div class=comment-body id=div-comment-11828><div class="comment-author vcard"><cite class=fn> Steven Z </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-11828> 08/05/2012 at 8:11 pm </a></div><p>I like simple but accurate things</div><li class="comment even thread-even depth-1"id=comment-12379><div class=comment-body id=div-comment-12379><div class="comment-author vcard"><cite class=fn> Venkata </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-12379> 30/05/2012 at 10:52 pm </a></div><p>good explanation,<p>and the best part is the usage of jargon is minimized. Generally its not the case with other resources.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-12505><div class=comment-body id=div-comment-12505><div class="comment-author vcard"><cite class=fn> Shafaet </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-12505> 05/06/2012 at 2:50 am </a></div><p>Good explanation,Thank you. It would be great if you can add advantages of adapters.</div><li class="comment even thread-even depth-1"id=comment-12521><div class=comment-body id=div-comment-12521><div class="comment-author vcard"><cite class=fn> Sam </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-12521> 05/06/2012 at 1:47 pm </a></div><p>good one</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-12915><div class=comment-body id=div-comment-12915><div class="comment-author vcard"><cite class=fn> Tribhuwan </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-12915> 18/06/2012 at 7:48 pm </a></div><p>Really Good example and precise explanation, First time i saw few blogs of yours…. Thank You.</div><li class="comment even thread-even depth-1"id=comment-13300><div class=comment-body id=div-comment-13300><div class="comment-author vcard"><cite class=fn> patrick </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-13300> 02/07/2012 at 3:59 pm </a></div><p>hi<p>i highly appreciate this simple examples ,kindly give a more complex example of a real world situation in a system<p>example a program to save use contacts and show how the adapter pattern can be used.<p>because a so simple example leaves the reader confused on how to apply the pattern in a real world application</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-13433><div class=comment-body id=div-comment-13433><div class="comment-author vcard"><cite class=fn> Ramani </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-13433> 06/07/2012 at 2:59 pm </a></div><p>Adapter implementation using Aggregation will be more correct than Composition as the Socket can exist without the Adaptor, or the Socket is not destroyed if the Adaptor is destroyed.<p><a rel="nofollow ugc"href=https://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/> https://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/ </a></div><li class="comment even thread-even depth-1"id=comment-14904><div class=comment-body id=div-comment-14904><div class="comment-author vcard"><cite class=fn> Anju Rattan </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-14904> 22/08/2012 at 2:16 pm </a></div><p>Good and very simple to understand explaination of Adapter Patterns..Helped me alot for my assigment in South Africa University.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-15610><div class=comment-body id=div-comment-15610><div class="comment-author vcard"><cite class=fn> divya </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-15610> 07/09/2012 at 10:36 am </a></div><p>thnx fr such a grt tutorial.. <br> its really beneficial..</div><li class="comment even thread-even depth-1"id=comment-15987><div class=comment-body id=div-comment-15987><div class="comment-author vcard"><cite class=fn> Gaurav </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-15987> 15/09/2012 at 5:19 pm </a></div><p>Very nice and cool example. i have learn 2 things from above explanation – <br> 1 – What is adapter class <br> 2 – What is is-a and has-a relationship</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-16553><div class=comment-body id=div-comment-16553><div class="comment-author vcard"><cite class=fn> devendra </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-16553> 28/09/2012 at 12:09 am </a></div><p>Very good example</div><li class="comment even thread-even depth-1"id=comment-17044><div class=comment-body id=div-comment-17044><div class="comment-author vcard"><cite class=fn> Ed </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-17044> 08/10/2012 at 10:29 pm </a></div><p>This blog is simple and amazing. Nothing else to say.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-17525><div class=comment-body id=div-comment-17525><div class="comment-author vcard"><cite class=fn> Ganesh </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-17525> 18/10/2012 at 2:23 pm </a></div><p>The example code and explanation is good. <br> I am able to understand the pattern and the concepts of compatible and incompatible from the socket perspective like rectangular or cylindarical. Consider a different scenario (not socket). From a Java programming perspective or OOPS perspective. On what basis, you will say a method is compatible or incompatible?. Is it based on the number of arguments and its types? Please explain with that scenario.</div><li class="comment even thread-even depth-1"id=comment-17654><div class=comment-body id=div-comment-17654><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-17654> 22/10/2012 at 11:28 am </a></div><p>nice artical</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-18158><div class=comment-body id=div-comment-18158><div class="comment-author vcard"><cite class=fn> Manu </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-18158> 29/10/2012 at 7:38 am </a></div><p>Nice Stuff.Deeply Appreciated</div><li class="comment even thread-even depth-1"id=comment-19821><div class=comment-body id=div-comment-19821><div class="comment-author vcard"><cite class=fn> Guo SONG </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-19821> 04/12/2012 at 6:26 am </a></div><p>errors of codes are everywhere!</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-20811><div class=comment-body id=div-comment-20811><div class="comment-author vcard"><cite class=fn> Siddhanta Pattnaik </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-20811> 25/12/2012 at 10:19 am </a></div><p>Through composition we can inject CylindricalSocket through dependency injection no need to create object of CylindricalSocket everytime .<p>public class RectangularAdapter { <br> @Autowire <br> private CylindricalSocket socket;<p>public String adapt(String rectaStem1, Sting rectaStem2) { <br> //some conversion logic <br> //socket = new CylindricalSocket(); <br> //no need to create object here <br> String cylinStem1 = rectaStem1; <br> String cylinStem2 = rectaStem2; <br> return socket.supply(cylinStem1, cylinStem2); <br> } <br> }<p>create object in application-context.xml</div><li class="comment even thread-even depth-1"id=comment-20812><div class=comment-body id=div-comment-20812><div class="comment-author vcard"><cite class=fn> Siddhanta Pattnaik </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-20812> 25/12/2012 at 10:27 am </a></div><p>Very Very Good tutorial…</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-21194><div class=comment-body id=div-comment-21194><div class="comment-author vcard"><cite class=fn> Pallavi </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-21194> 04/01/2013 at 10:52 am </a></div><p>Thank q so much for ure wonderful tutorials.. they r so simple to understand. Every topic that i look for i first visityour website.<p>I just wanted to ask one doubt. In struts too adaptor pattern is followed ? Could you plz explain which component follows it and Y?</div><li class="comment even thread-even depth-1"id=comment-21220><div class=comment-body id=div-comment-21220><div class="comment-author vcard"><cite class=fn> J. </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-21220> 04/01/2013 at 8:17 pm </a></div><p>Good work, thanks!</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-22482><div class=comment-body id=div-comment-22482><div class="comment-author vcard"><cite class=fn> pank </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-22482> 26/01/2013 at 11:21 pm </a></div><p>Hi Joe, <br> Your tutorials are really nice.<p>But, please correct me if i am wrong…. ,<p>getPower() method should have return type…. isn’t it? e.g in this case void.</div><li class="comment even thread-even depth-1"id=comment-22484><div class=comment-body id=div-comment-22484><div class="comment-author vcard"><cite class=fn> pank </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-22484> 26/01/2013 at 11:37 pm </a></div><p>In your CylinderSocket class, how supply method can have String as return type?? …. :S</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-23770><div class=comment-body id=div-comment-23770><div class="comment-author vcard"><cite class=fn> Nikhil </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-23770> 20/02/2013 at 9:24 pm </a></div><p>Thanks, Very nice explanation… <br> I also heard of adapter pattern is used when some where we don’t want to implement all methods of interfaces <br> for example :<p>interface Myinterface{ <br> public void mymethodeone(); <br> public void mymethodeone(); <br> }<p>abstract class Myadapter implements Myinterface {<p>public void mymethodeone(){} <br> public void mymethodeone(){} <br> }<p>class Myclass extends Myadapter <br> { <br> public void mymethodeone(){ <br> //my implementation here <br> } <br> }<p>is this also a adapter pattern?</div><li class="comment even thread-even depth-1"id=comment-23773><div class=comment-body id=div-comment-23773><div class="comment-author vcard"><cite class=fn> Habibi </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-23773> 20/02/2013 at 11:14 pm </a></div><p>Well done Joe. Congo.</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-24179><div class=comment-body id=div-comment-24179><div class="comment-author vcard"><cite class=fn> Srikrishna </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-24179> 27/02/2013 at 11:43 am </a></div><p>Sir, <br> You are awesome and all your content is very helpful.I am a .Net guy.But always refer your site for any understanding points very clearly. <br> Thanks sir</div><li class="comment even thread-even depth-1"id=comment-24446><div class=comment-body id=div-comment-24446><div class="comment-author vcard"><cite class=fn> sima </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-24446> 03/03/2013 at 9:39 pm </a></div><p>ya really</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-24845><div class=comment-body id=div-comment-24845><div class="comment-author vcard"><cite class=fn> Jay </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-24845> 12/03/2013 at 11:12 pm </a></div><p>Thanks for this easy explanation and example!</div><li class="comment even thread-even depth-1"id=comment-25035><div class=comment-body id=div-comment-25035><div class="comment-author vcard"><cite class=fn> Amol </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-25035> 16/03/2013 at 8:06 pm </a></div><p>Nice one!!!! <br> I want to suggest some modifications though.<p>RectangularAdapter should have ‘supply’ method rather than ‘adapt’ method. RectangularPlug class should be completely unaware RectangularAdapter class. Please see below code. (I am not able to indent code properly in textarea)<p>class CylindricalSocket { <br> public supply(String cylinStem1,String cylinStem2) { <br> return(“Power power power…”); <br> } <br> }<p>class RectangluarSocket{ <br> public supply(String rectaStem1, String rectaStem2) { <br> return(“Power power power…”); <br> } <br> } <br> class CylindricalSocketAdapter extends RectangluarSocket{ <br> private CylindricalSocket cylindricalSocket = null; <br> public CylindricalSocketAdapter(CylindricalSocket socket){ <br> this.cylindricalSocket = socket; <br> } <br> public String supply(String rectaStem1,String rectaStem2) { <br> String cylinStem1 = rectaStem1; <br> String cylinStem2 = rectaStem2; <br> return cylindricalSocket.supply(cylinStem1, cylinStem2); <br> } <br> }<p>//Rectangluar plug knows only about RectangluarSocket. It does not know about any adapter. IT only knows about RectangularPlug<p>class RectangularPlug { <br> private RectangluarSocket rectangluarSocket = null; <br> private String rectaStem1; <br> private String rectaStem2; <br> public RectangularPlug(RectangluarSocket socket){ <br> this.rectangluarSocket = socket; <br> } <br> public void getPower() { <br> String power = rectangluarSocket.supply(rectaStem1, rectaStem2); <br> System.out.println(power); <br> } <br> } <br> new RectangularPlug(new CylindricalSocketAdapter(new CylindricalSocketAdapter()));</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-26215><div class=comment-body id=div-comment-26215><div class="comment-author vcard"><cite class=fn> jj </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-26215> 30/03/2013 at 5:46 pm </a></div><p>what I understand is : java.io.InputStreamReader(InputStream) <br> java.io.OutputStreamWriter(OutputStream) implements Decorator Pattern, please comment!</div><li class="comment even thread-even depth-1"id=comment-28916><div class=comment-body id=div-comment-28916><div class="comment-author vcard"><cite class=fn> rahul srivastava </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-28916> 20/04/2013 at 2:42 pm </a></div><p>Great explanation and awesome real life example .Just one question more I am having is that when we should use composition approach and when to use inheritance one for this pattern</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-30422><div class=comment-body id=div-comment-30422><div class="comment-author vcard"><cite class=fn> Rania </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-30422> 02/05/2013 at 12:13 pm </a></div><p>Sir I have stil confusions . Any body help me out please ??</div><li class="comment even thread-even depth-1"id=comment-31768><div class=comment-body id=div-comment-31768><div class="comment-author vcard"><cite class=fn> Garfield </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-31768> 22/05/2013 at 12:04 pm </a></div><p>The best example I could find :-)</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-31769><div class=comment-body id=div-comment-31769><div class="comment-author vcard"><cite class=fn> Garfield </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-31769> 22/05/2013 at 12:09 pm </a></div><p>But holy sh… there are about 10 errors in the example. Have you written that in notepad?!</div><li class="comment even thread-even depth-1"id=comment-34627><div class=comment-body id=div-comment-34627><div class="comment-author vcard"><cite class=fn> GAK </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-34627> 27/06/2013 at 7:08 pm </a></div><p>Client : RectangularPlug <br> Adapter : RectangularAdapter <br> Adaptee : CylindericalSocket</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-34630><div class=comment-body id=div-comment-34630><div class="comment-author vcard"><cite class=fn> GAK </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-34630> 27/06/2013 at 7:20 pm </a></div><p>Use class adapter:IS -A,when <br> Adapter override somw of adaptees behviour,since Adpater is a subclass of Adptee <br> Example WindowListener, java.awt.event.WindowAdapter<p>Use Object adapter:Has -A,when <br> a single adapter work with many adapters(adaptee and its subclasses)</div><li class="comment even thread-even depth-1"id=comment-68363><div class=comment-body id=div-comment-68363><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-68363> 13/02/2014 at 2:41 am </a></div><p>really nice explanation</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-73220><div class=comment-body id=div-comment-73220><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-73220> 13/03/2014 at 2:22 pm </a></div><p>Very easy to understand… Extremely useful. Could understand in very few mins…</div><li class="comment even thread-even depth-1"id=comment-74075><div class=comment-body id=div-comment-74075><div class="comment-author vcard"><cite class=fn> Anonymous </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-74075> 17/03/2014 at 8:59 pm </a></div><p>Why it is so complicated ? <br> It is simple<p>ClassA has-a ClassB to to process<p>How ClassC can make fit to ClassA for the process like Class B<p>Adapt it<p>ClassA a = new ClassA(new ClassB()) <br> ClassA a1 = new ClassA(new ClassBApdator(new ClassC())</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-76625><div class=comment-body id=div-comment-76625><div class="comment-author vcard"><cite class=fn> NeeleshK </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-76625> 26/03/2014 at 9:19 pm </a></div><p>Thanks for the article. A trivia- British plugsare not cylindrical AFAIK. They are rectangular too, just the rectangles are bigger size than the ones in American plugs. Indian plugs are cylindrical.<p>Thanks <br> Neelesh</div><li class="pingback even thread-even depth-1"id=comment-78079><div class=comment-body id=div-comment-78079><div class="comment-author vcard"><cite class=fn> <a rel="external nofollow ugc"class=url href=https://javapapers.com/design-patterns/proxy-design-pattern/> Proxy Design Pattern </a> </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-78079> 31/03/2014 at 11:34 pm </a></div><p>[…] Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object. […]</div><li class="comment odd alt thread-odd thread-alt depth-1"id=comment-87991><div class=comment-body id=div-comment-87991><div class="comment-author vcard"><cite class=fn> Kshitij </cite><span class=says> says: </span></div><div class="comment-meta commentmetadata"><a href=https://javapapers.com/design-patterns/adapter-pattern/#comment-87991> 24/04/2014 at 4:46 pm </a></div><p>Good explanation…</div></ol><p class=no-comments>Comments are closed for "Adapter Design Pattern".</div></div></div><div id=footer><div class=content><div class=subscribe-section><form onsubmit="window.open('https://feedburner.google.com/fb/a/mailverify?uri=javapapers/java-interview-questions',
             'popupwindow', 'scrollbars=yes,width=550,height=520');return true"action=https://feedburner.google.com/fb/a/mailverify method=post target=popupwindow><div><label for=feedb-email-inp>Subscribe:</label></div><input placeholder="Enter your email here"class=feedb-email id=feedb-email-inp name=email><input name=uri type=hidden value=javapapers/java-interview-questions><input name=loc type=hidden value=en_US><input class=subscribe type=submit value=Subscribe></form><div class=follow><a rel="nofollow noopener"title="Javapapers Facebook Page"class=facebook href=https://www.facebook.com/javapapers target=_blank> Javapapers Facebook Page </a></div></div><p class=copyright>© 2008 - 2019 <a title="Android & Java Blog"href=/> Javapapers </a></div></div><script src=https://javapapers.com/wp-content/themes/papers/prettify.js></script>