Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

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.

![]() |
Site Address Prob
|
VB mouseover image
|

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