473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Extending java.util.Stack?

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
4 5593
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dominique | last post by:
I am trying to communicate to a prolog server from a java client, however even though the connection is successfully made every time I try to perform QueryExecute I get an error, either Socket...
1
by: timothy ma and constance lee | last post by:
Sir I got the design problem how to implement the stack in java. I want to have a stack or list to store a page no. with its pointers. The pointers may comprise of a set of strings like...
1
by: greg.knaddison | last post by:
Hi, I'm trying to use the httpclient within Jython (see http://jakarta.apache.org/commons/httpclient/ for more information on the httpclient). My Jython version is: Jython 2.1 on...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
8
by: gimme_this_gimme_that | last post by:
I have the following Java code : package com.rhi.bb.udf.utils; import java.sql.Clob; import java.sql.SQLException; import java.util.regex.Pattern; import java.util.regex.Matcher;
0
by: snkssa | last post by:
Hi, I got the following error while building an application with ant. please can you give the solution or reason why it is failing? Thank you create_zip_linux: ...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
8
by: ajos | last post by:
hi frnds, im trying to convert my servlets database configuration from ms access to mysql database.however im getting some error like no driver found exception. to verify this error ive...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.