aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What does Wrapper classes mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What does Wrapper classes mean?" Watch "What does Wrapper classes mean?" New topic
Author

What does Wrapper classes mean?

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
What dose Wrapper classes mean?
And what should I know about them in the SCJP exam
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I'll answer your first question -- a wrapper class (also called a 'decorator class') wraps (sic) another object and adds a bit of functionality to it, without the client knowing of this all.
Suppose an interface 'Window' exists:

Also suppose you want four additional features using these windows (and combinations thereof):
1) plain windows;
2) windows with borders;
3) scrollable windows;
4) windows with a title on top.
They all implement the interface 'Window' of course. If you just go for it, you'll find yourself implementing 16 (2**4) different classes:
plain windows, windows with borders, windows with scrollbars, windows with borders and scrollbars, windows with titles, windows with titles and borders etc. etc. and finally windows with borders, scrollbars and titles on top.
This is not the way to go. Here's a skeleton for the windows with border class, implemented as a wrapper:

I guess you can come up with a wrapper for WindowsWithScrollbars? Given these two wrapperclasses it is trivial to create a window with borders and scrollbars:

Can you see the wrappings?
kind regards
[ January 26, 2003: Message edited by: Jos Horsmeier ]
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
What Jos has written is some very interesting info on real world use of Wrappers, cool
For the exam, you might need to know stuff like...
the wrapper classes Boolean, Byte, Character, Short, Integer, Long, Float, and Double, are the corresponding wrapper classes for the primitive data types. Character class wraps up a char. Float class wraps up a float value (by 'wraps' i mean, makes an object out of it).
These wrapper classes come from the java.lang package, they are final - in that they may not be subclassed. so class new extends Integer { // will produce compile-time error.
to wrap a primitive value into an object, check this./..
int primitInt = 167;
Integer wrappedUpInt = new Integer(primitInt);
primitInt is still a primitive value, but there is also an object of type Integer in existence (not of type int, although it is passed an int when the constructor is called, as a constructor for Short would be passed a short, the constructor for Character would be passed a char).


giddee up
Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
Well,thank you so much.
It will take me some time to understand it.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jasper Vader:

These wrapper classes come from the java.lang package, they are final - in that they may not be subclassed. so class new extends Integer { // will produce compile-time error.

Besides "new" is a reserved word, so it can't be used as a class name anyway.


Java API Documentation
The Java Tutorial
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Doh!

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to The Programmer Certification Forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Ths SG.
Your displayed name does not comply with our name policy . Please take a moment to change it here


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does Wrapper classes mean?