473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*Ret ention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
Jul 17 '05 #1
4 5616
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.2187 8$Ig.15377@pd7t w2no...
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*Ret ention*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
interoperabilit y 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.s pam.com.spam> escribió en el
mensaje news:%%******** **********@twis ter.nyroc.rr.co m...
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
interoperabilit y 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*Ret ention*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
2699
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 closed or null etc. Can anyone help? I am using SICStus prolog. Currently my prolog server looks like: :- write('starting'),nl. :- use_module(library(sockets)). :- use_module(library(prologbeans)). :- use_module(library(charsio)).
1
4406
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 timestamp, date etc. In the stack, it can be searched, insert, delete. Please help or provide some reference
1
47611
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 java1.4.2_04 (JIT: null) My Java version is:
18
7735
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); System.out.println(xp.evaluate(new InputSource(new FileInputStream("a.xml"))));
8
5008
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
1880
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
3279
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 1.5.0_11-b03, mixed mode, sharing)) and in both cases, get the following list returned from calling getDeclaredFields() on java.lang.ClassLoader via this code snippet: Field fields = loaderClass.getDeclaredFields(); for (int i = 0; i <...
0
3281
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
28580
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 made a simple database in jsp(just to check if my mysql is working smoothly otherwise) which access' the username.....but in the process im getting a error--> java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) i have...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.