What is a wrapper class?
Asked Answered
M

17

248

What is a wrapper class?
How are such classes useful?

Misalliance answered 20/5, 2009 at 17:16 Comment(0)
D
184

In general, a wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. These are useful by providing a level of abstraction from the implementation of the underlying class or component; for example, wrapper classes that wrap COM components can manage the process of invoking the COM component without bothering the calling code with it. They can also simplify the use of the underlying object by reducing the number interface points involved; frequently, this makes for more secure use of underlying components.

Dambrosio answered 20/5, 2009 at 17:19 Comment(4)
A wrapper class (as adapter) is used when the wrapper must respect a particular interface and must support a polymorphic behavior. On the other hand, a facade is used when one wants an easier or simpler interface to work with.Kingsize
In this post, it is proposed that a wrapper could be useful for creating a strictly-defined proxy for a late-bound class: Can I use late binding.... Is this a valid additional use for wrappers beyond what Paul Sonier described above?Galibi
@NirajanSingh Wrapper reuses two already existing interfaces and makes one work with the other. On the other hand, facade creates a new interface.Inn
Voted down! What about adding a code example and use code instead of just words?Cube
S
72

Just what it sounds like: a class that "wraps" the functionality of another class or API in a simpler or merely different API.

See: Adapter pattern, Facade pattern

Sty answered 20/5, 2009 at 17:18 Comment(1)
Yes! Frequently a synonimous for Facade pattern. In many code documentation I see evident Facade method, described as "Wrapper to XX::yy method" or a little adiction like "Wrapper to XX::yy method with exception handling".Census
K
43

Wrapper classes provide a way to use primitive types as objects. For each primitive , we have a wrapper class such as,

int Integer
byte Byte 

Integer and Byte are the wrapper classes of primitive int and byte. There are times/restrictions when you need to use the primitives as objects so wrapper classes provide a mechanism called as boxing/unboxing.

Concept can be well understood by the following example as

double d = 135.0 d;
Double doubleWrapper = new Double(d);

int integerValue = doubleWrapper.intValue();
byte byteValue = doubleWrapper.byteValue();
string stringValue = doubleWrapper.stringValue();

so this is the way , we can use wrapper class type to convert into other primitive types as well. This type of conversion is used when you need to convert a primitive type to object and use them to get other primitives as well.Though for this approach , you need to write a big code . However, the same can be achieved with the simple casting technique as code snippet can be achieved as below

double d = 135.0;
int integerValue = (int) d ;

Though double value is explicitly converted to integer value also called as downcasting.

Kalgoorlie answered 2/7, 2012 at 10:14 Comment(3)
Your answer implies wrapper classes are only for primitive objects.Lightless
Your answer is only valid in the strict Java terminology, whilst the original question was language agnostic.Hexavalent
Nb. using Double instead of double in C# makes no difference, they are exactly the same thing.Anchoress
I
19

A wrapper class doesn't necessarily need to wrap another class. It might be an API class wrapping functionality in e.g. a dll file.

For example, it might be very useful to create a dll wrapper class, which takes care of all dll initialization and cleanup and create class methods that wrap function pointers created from e.g. GetProcAddress().

Ilanailangilang answered 20/5, 2009 at 17:21 Comment(0)
J
9

There are several design patterns that can be called wrapper classes.

See my answer to "How do the Proxy, Decorator, Adaptor, and Bridge Patterns differ?"

Jawbreaker answered 20/5, 2009 at 17:16 Comment(0)
U
8

It might also be valuable to note that in some environments, much of what wrapper classes might do is being replaced by aspects.

EDIT:

In general a wrapper is going to expand on what the wrappee does, without being concerned about the implementation of the wrappee, otherwise there's no point of wrapping versus extending the wrapped class. A typical example is to add timing information or logging functionality around some other service interface, as opposed to adding it to every implementation of that interface.

This then ends up being a typical example for Aspect programming. Rather than going through an interface function by function and adding boilerplate logging, in aspect programming you define a pointcut, which is a kind of regular expression for methods, and then declare methods that you want to have executed before, after or around all methods matching the pointcut. Its probably fair to say that aspect programming is a kind of use of the Decorator pattern, which wrapper classes can also be used for, but that both technologies have other uses.

Unbidden answered 20/5, 2009 at 17:43 Comment(0)
F
7

A wrapper class is a class that "wraps" around something else, just like its name.

The more formal definition of it would be a class that implements the Adapter Pattern. This allows you to modify one set of APIs into a more usable, readable form. For example, in C#, if you want to use the native Windows API, it helps to wrap it into a class that conforms to the .NET design guidelines.

Fasciculus answered 20/5, 2009 at 17:18 Comment(1)
By my understanding of the adapter pattern, it's different from a wrapper: if X wraps Y, then X should encapsulate the state of Y, but not its identity. Given two instances of X, both of which wrap a Y, there should be no means by which one could prove that both instances of X wrap the same instance of Y. By contrast, an adapter object will often encapsulate identity but not state; a change to the object being adapted would not be considered a change to the adapter itself.Schick
M
7

Wrapper class is a class that wrap the another class and provide the abstraction between client and the original class being wrapped.

Mcclellan answered 9/9, 2011 at 5:11 Comment(0)
T
6

a wrapper class is usually a class that has an object as a private property. the wrapper implements that private object's API and so it can be passed as an argument where the private object would.

say you have a collection, and you want to use some sort of translation when objects are added to it - you write a wrapper class that has all the collection's methods. when add() is called, the wrapper translate the arguments instead of just passing them into the private collection.

the wrapper can be used anyplace a collection can be used, and the private object can still have other objects referring to it and reading it.

Tayyebeb answered 20/5, 2009 at 17:25 Comment(3)
Agree, my personal view on wrappers is that they look and feel like the original type and may implement the same interface, definately hold state of the wrapped instance. They exist as a way to "intercept" calls to members.Juliettajuliette
Very helpful. I would like to see this coded up.Souterrain
something like ``` class MyCol implements Col { private inner: Col; MyCol(inner: Col){ this.inner = inner; } @Override public add(a: Int){ this.inner.add(a + 1); } }Tayyebeb
W
3

Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding class instances e.g. a boolean data type can be represented as a Boolean class instance. All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.

Whidah answered 4/7, 2011 at 8:14 Comment(0)
O
3

Java programming provides wrapper class for each primitive data types, to convert a primitive data types to correspond object of wrapper class.

Ornithology answered 1/5, 2012 at 12:42 Comment(0)
S
2

A wrapper class is a class that is used to wrap another class to add a layer of indirection and abstraction between the client and the original class being wrapped.

Sampler answered 20/5, 2009 at 17:18 Comment(0)
S
1

To make a wrapper class well being is not a easy job. To understand a wrapper class how it is designed by some others is also not a easy job. Because it is idea, not code. Only when you understand the idea, you can understand wrapper.

Steib answered 18/11, 2014 at 3:14 Comment(0)
O
1

I currently used a wrapper class for my project and the main benefits I get (just a single benefit to widen the topic explanation):

Exception handling: My main class ,that another class wraps, has methods that are throwing exceptions if occurs any, so I created a wrapper class that handles the exceptions and logs them immediately. So, in my main scope, there is no exception handling. I just call a method and do something.

Easy Usage: I can easily initiate the object. Normally initiating phase is constructed of a lot of steps.

Code Readability: When another programmer opens my code, the code will seem very clear and easy to manipulate.

Hiding the Details: If you are generating a class that another programmer is going to use, then you can wrap the details like "error handling, exception handling, logging messages and etc..." so that the programmer will not be have to handle the chaos, just simply uses the methods.

Osteo answered 20/11, 2015 at 9:27 Comment(0)
G
1

A wrapper class is a class that serves the sole purpose of holding something and adding some functionality to it. In Java since the primitives (like ints,floats,chars...) are not objects so if you want to treat them like one then you have to use a wrapper class. Suppose you want to create a Vector of ints, the problem is a Vector only holds Objects not primitives. So what you will do is put all the ints in an Integer wrapper and use that. Example:

int number = 5;
Integer numberWrapped = new Integer(number);
//now you have the int in an object.
//and this is how to access the int value that is being wrapped.
int again = numberWrapped.intValue();
Gust answered 14/12, 2015 at 14:9 Comment(1)
This is known as boxing primitives rather than wrapping.Glasswort
N
0

Wrapper classes came in to existence to fulfill a basic need of programmers - i.e. to use primitive values wherever only Objects are allowed. As their name suggests wrapper classes wrap around a primitive value and hold the value in an Object. So, all those places where primitives were not allowed - such as generics, hashmap-keys, Arraylists etc - programmers now have an option of providing these primitive values as their corresponding wrapper types.

In addition these wrapper types have a number of utility methods for converting from primitive type to corresponding wrapper types and back, and also from Strings to wrapper types and back.

I have written a detailed article on wrapper classes in a my blog which explains the concept of wrapper types in depth - http://www.javabrahman.com/corejava/java-wrapper-classes-tutorial-with-examples/ (Disclosure - This blog is owned by me)

Norine answered 3/11, 2015 at 6:23 Comment(0)
D
-8

a wrapper is class which is used to communicate between two different application between different platform

Dissipated answered 27/9, 2011 at 11:46 Comment(3)
I think you can use it for a lot more than just that one caseGarrick
Indeed, this is only one small use case.Revolver
I think this is the definition of the web servicesOsteo

© 2022 - 2024 — McMap. All rights reserved.