By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

Extending java.util.Stack?

P: n/a
Hello,
If I want to make a class which is a stack of objects of some specific type
(not just "Objects"), what is the best way to do it?

I wonder if there is some generally accepted "best" way to do this, because
it must come up a lot.

I personally create a new class which mimics all the methods of
java.util.Stack , and keeps a java.util.Stack as a private instance
variable. The new class changes the return types of pop and peek and the
argument type of push. Is this the most elegant solution?

I'm a student programmer, and appreciate any insight.

Adam
-----------------
www.Newsgroup-Binaries.com - *Completion*Retention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
NadaHombre wrote:
Hello,
If I want to make a class which is a stack of objects of some specific type
(not just "Objects"), what is the best way to do it?

I wonder if there is some generally accepted "best" way to do this, because
it must come up a lot.

I personally create a new class which mimics all the methods of
java.util.Stack , and keeps a java.util.Stack as a private instance
variable. The new class changes the return types of pop and peek and the
argument type of push. Is this the most elegant solution?

I'm a student programmer, and appreciate any insight.


I don't know if you have to do this, but why not just simply do a cast?

ex:
MyObject o = new MyObject();
stack.push( o );

MyObject pushedObject = (MyObject) stack.pop();

Jul 17 '05 #2

P: n/a
"Yoyoma_2" <Yoyoma_2@[at-]Hotmail.com> escribió en el mensaje
news:nNNbc.21878$Ig.15377@pd7tw2no...
NadaHombre wrote:
Hello,
If I want to make a class which is a stack of objects of some specific type (not just "Objects"), what is the best way to do it?

I wonder if there is some generally accepted "best" way to do this, because it must come up a lot.

I personally create a new class which mimics all the methods of
java.util.Stack , and keeps a java.util.Stack as a private instance
variable. The new class changes the return types of pop and peek and the argument type of push. Is this the most elegant solution?

I'm a student programmer, and appreciate any insight.


I don't know if you have to do this, but why not just simply do a cast?

ex:
MyObject o = new MyObject();
stack.push( o );

MyObject pushedObject = (MyObject) stack.pop();


Thanks for the response.

I understand that this will work, but it seems like bad practice to me. If
I use this method, I have to be careful that I never push anything but a
MyObject--the stack will not enforce this restriction for me. And if my
program does have a bug, and pushes something that is not an instance of
MyObject, it will cause a class-cast exception in a completely different
part of my program. In theory, if this were a big project, the exception
could come up in a module that was written by a different programmer than
the module that did the offending push in the first place.
-----------------
www.Newsgroup-Binaries.com - *Completion*Retention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
Jul 17 '05 #3

P: n/a
NadaHombre wrote:
Hello,
If I want to make a class which is a stack of objects of some specific type
(not just "Objects"), what is the best way to do it?

I wonder if there is some generally accepted "best" way to do this, because
it must come up a lot.

I personally create a new class which mimics all the methods of
java.util.Stack , and keeps a java.util.Stack as a private instance
variable. The new class changes the return types of pop and peek and the
argument type of push. Is this the most elegant solution?

I'm a student programmer, and appreciate any insight.


Your approach is sound and common. Extending Stack is not the best
choice, because you cannot hide the public methods from Stack that
accept Objects so you cannot ensure that your internal stack is homogeneous.

Now, a couple of notes and suggestions, since you are a student.

1) Stack is an extension of Vector, which is a synchronized class.
Unless you have a specific need for synchronization in your application,
this is overhead you do not need. Instead use the LinkedList class as
the internal reference.

2) Does your implementation implement List? If not, that would be a
good improvement that would allow for future re-usability and
interoperability with other libraries. (Suggestion, if you are going to
implement List, the easier way is to extend AbstractList.)

3) Note that in Java 1.5 (coming soon), you will be able to have
so-called "generics", which are similar to C++ templates. This will
make the need for such implementations disappear.

Ray
Jul 17 '05 #4

P: n/a
"Raymond DeCampo" <rd******@spam.twcny.spam.rr.spam.com.spam> escribió en el
mensaje news:%%******************@twister.nyroc.rr.com...
NadaHombre wrote:
Hello,
If I want to make a class which is a stack of objects of some specific type (not just "Objects"), what is the best way to do it?

I wonder if there is some generally accepted "best" way to do this, because it must come up a lot.

I personally create a new class which mimics all the methods of
java.util.Stack , and keeps a java.util.Stack as a private instance
variable. The new class changes the return types of pop and peek and the argument type of push. Is this the most elegant solution?

I'm a student programmer, and appreciate any insight.

Your approach is sound and common. Extending Stack is not the best
choice, because you cannot hide the public methods from Stack that
accept Objects so you cannot ensure that your internal stack is

homogeneous.
Now, a couple of notes and suggestions, since you are a student.

1) Stack is an extension of Vector, which is a synchronized class.
Unless you have a specific need for synchronization in your application,
this is overhead you do not need. Instead use the LinkedList class as
the internal reference.

2) Does your implementation implement List? If not, that would be a
good improvement that would allow for future re-usability and
interoperability with other libraries. (Suggestion, if you are going to
implement List, the easier way is to extend AbstractList.)

3) Note that in Java 1.5 (coming soon), you will be able to have
so-called "generics", which are similar to C++ templates. This will
make the need for such implementations disappear.

Ray


Cool! Thanks a lot.

Adam
-----------------
www.Newsgroup-Binaries.com - *Completion*Retention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.