• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper vs Adapter

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the differnece between Adapter and Wrapper classes? Whether both are same or different? If different, can you please give one example for it?
I know about adapter class. For ex:


so that your class can extend adapter class, rather than implement interface a and provide implementation for all its method.

Can you please explain on wrapper?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
these are classes defined in java.lang package (one class per primitive type) which can be used to wrap around primitive types so that the primitive types can be used where an reference to an object is required.

as for example if you are willing to make a Vector of ints, you cannot add an int to it, but you can add the int by wrapping it inside an Integer class.

the wrapper classes also provide static methods which can be used to extract properties of the primnitive type, like one can find the maximum value an int can hold by calling a static method of Integer class.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several meanings of adapter; note the unusual spelling. There is a design pattern of that name, and there are abstract classes which implement a Listener interface (eg MouseAdapter).

You can also have a wrapper which mimics the methods of another class. You can have a class with [EMAIL]n[/EMAIL] methods, and you have another class with n methods with exactly the same names. The second class "wraps" an instance of the first class, so it is a "wrapper" to the other class.
Each method of the wrapper does some sort of controlling code, then calls the method of the same name in the other class.

You have probably got wrappers answered; adapters probably are too complicated for the beginner's forum: moving.
 
subhajit paul
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does that mean that even inheritance is some kind of wrapping?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by subhajit paul:
does that mean that even inheritance is some kind of wrapping?



No, I wouldn't consider inheritance to be wrapping. Wrappers usual work on a different object, while inheritance (use of super.xxx()) works on the same object.


versus

[ December 18, 2008: Message edited by: Steve Luke ]
 
Mike Thomson
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still I am not getting on wrapper. Any small piece of code explaining this will helpful.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already said above, a wrapper class is something that wraps another class to provide methods/functionality/interface usually for integration.

A very simple but effective example is that of the wrapper classes for primitive data types. You might have used the following piece of code atleast once in your programming.

Integer.parseInt("some string representing a numeric value");

Now since you cannot have methods for the int primitive type, the Integer class is handy in such situations. Also if you are familiar with Collections, then all of the collections are meant to hold objects and one cannot place integer or double values in them without using a wrapper or a string representation of that value.

Now this is also far different from inheritance as there is no way to inherit the primitives or say final classes.But nothing stops you from wrapping them .


Hope this helps
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You see there are 4 methods in the Engine interface, and there are 4 methods in the Car class which are very similar. They simply call the Engine methods. You are using the Car as a Wrapper for the Engine; by calling a Car method you are simply passed to the Engine.This will appear like an ordinary List, but each method does something special. It has an ordinary List as a field, and each method calls the List method of the same name. Each method adds some special functionality to the List; it doesn't matter what, but there is (I think) an example in Joshua Bloch's Effective Java where each method adds thread-locking.
[edit]Minor changes to method names[/edit]
[ December 19, 2008: Message edited by: Campbell Ritchie ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
There are several meanings of adapter; note the unusual spelling.



In US English "adapter" is more common. I thought "adaptor" was a Britishism, like "colour" or "aluminium".
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never realised that about spelling. Thank you.

Or should it be . . .

I never realized that about spelling. Thank you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic