Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does Wrapper classes mean?

 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What dose Wrapper classes mean?
And what should I know about them in the SCJP exam
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,thank you so much.
It will take me some time to understand it.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh!

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to The Programmer Certification Forum...
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Ths SG.
Your displayed name does not comply with our name policy . Please take a moment to change it here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic