Computing.Net > Forums > Programming > java : wrapper class

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

java : wrapper class

Reply to Message Icon

Name: Min
Date: March 6, 2003 at 03:10:25 Pacific
OS: XP
CPU/Ram: p4
Comment:

Hmm.. Can someonw explain to me what is wrapper class and how it is used?



Sponsored Link
Ads by Google

Response Number 1
Name: gpp
Date: March 6, 2003 at 06:08:26 Pacific
Reply:

Well, some classes are very generic.. they use the class Object as the argument and result. Most of the time you'll want to use only one type of object, so rather than having to do an explicit type cast in every location that you call the classes methods, you can write a simple wrapper class that wraps the generic class with a more concrete class.

Example)

java.util.Stack

push() and search() take 'Object' as the argument

peek(), pop(), and push() return 'Object'

So you could write a wrapper class to force these methods to accept Strings(or some other object) and to return Strings(or some other object).

I hope this makes sense. I'm terrible at explaining these things!



0

Response Number 2
Name: min
Date: March 7, 2003 at 01:04:25 Pacific
Reply:

How would u actually do that..=p?


0

Response Number 3
Name: gpp
Date: March 7, 2003 at 05:37:55 Pacific
Reply:

This what the stack wrapper class might look like..

public class StackWrapper{

Stack stack;

StackWrapper() { stack=new Stack(); }

public boolean empty() { return stack.empty(); }
public void push(String string) { stack.push(string); }
public String pop() { return (String) stack.pop(); }
public String peek() { return (String) stack.peek(); }

}

Thats all there is to it... although you would probably want to name your class something a little more helpfull like StringStack.. or something that helps you remember what its a stack of.


0

Response Number 4
Name: min
Date: March 7, 2003 at 23:15:06 Pacific
Reply:

Thank you


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Site Address Prob VB mouseover image



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: java : wrapper class

Quick Java Question www.computing.net/answers/programming/quick-java-question/3555.html

java Method class www.computing.net/answers/programming/java-method-class/9980.html

Desperately Need Programming help! www.computing.net/answers/programming/desperately-need-programming-help/902.html