wood burning stoves 2.0*
The moose likes Java in General and the fly likes Wrapper vs Adapter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Wrapper vs Adapter" Watch "Wrapper vs Adapter" New topic
Author

Wrapper vs Adapter

Mike Thomson
Ranch Hand

Joined: Nov 07, 2007
Posts: 115
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?
subhajit paul
Greenhorn

Joined: Dec 17, 2008
Posts: 17
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
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

Joined: Dec 17, 2008
Posts: 17
does that mean that even inheritance is some kind of wrapping?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

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 ]

Steve
Mike Thomson
Ranch Hand

Joined: Nov 07, 2007
Posts: 115
Still I am not getting on wrapper. Any small piece of code explaining this will helpful.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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".


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
I never realised that about spelling. Thank you.

Or should it be . . .

I never realized that about spelling. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper vs Adapter
 
Similar Threads
removing duplicates from the list?
nested member classes and access to parents
problem in gc
different result - cast null to interface and class
Confusion in Garbage Collection