Extending java.util.Stack?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NadaHombre

    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
    -----------------
  • Yoyoma_2

    #2
    Re: Extending java.util.Stack ?

    NadaHombre wrote:[color=blue]
    > 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.[/color]

    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();

    Comment

    • NadaHombre

      #3
      Re: Extending java.util.Stack ?

      "Yoyoma_2" <Yoyoma_2@[at-]Hotmail.com> escribió en el mensaje
      news:nNNbc.2187 8$Ig.15377@pd7t w2no...[color=blue]
      > NadaHombre wrote:[color=green]
      > > Hello,
      > > If I want to make a class which is a stack of objects of some specific[/color][/color]
      type[color=blue][color=green]
      > > (not just "Objects"), what is the best way to do it?
      > >
      > > I wonder if there is some generally accepted "best" way to do this,[/color][/color]
      because[color=blue][color=green]
      > > 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[/color][/color]
      the[color=blue][color=green]
      > > argument type of push. Is this the most elegant solution?
      > >
      > > I'm a student programmer, and appreciate any insight.[/color]
      >
      > 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();
      >[/color]

      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
      -----------------

      Comment

      • Raymond DeCampo

        #4
        Re: Extending java.util.Stack ?

        NadaHombre wrote:[color=blue]
        > 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.
        >[/color]

        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

        Comment

        • NadaHombre

          #5
          Re: Extending java.util.Stack ?

          "Raymond DeCampo" <rdecampo@spam. twcny.spam.rr.s pam.com.spam> escribió en el
          mensaje news:%%Wbc.1526 1$LW2.9991@twis ter.nyroc.rr.co m...[color=blue]
          > NadaHombre wrote:[color=green]
          > > Hello,
          > > If I want to make a class which is a stack of objects of some specific[/color][/color]
          type[color=blue][color=green]
          > > (not just "Objects"), what is the best way to do it?
          > >
          > > I wonder if there is some generally accepted "best" way to do this,[/color][/color]
          because[color=blue][color=green]
          > > 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[/color][/color]
          the[color=blue][color=green]
          > > argument type of push. Is this the most elegant solution?
          > >
          > > I'm a student programmer, and appreciate any insight.
          > >[/color]
          >
          > 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[/color]
          homogeneous.[color=blue]
          >
          > 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[/color]

          Cool! Thanks a lot.

          Adam


          -----------------
          www.Newsgroup-Binaries.com - *Completion*Ret ention*Speed*
          Access your favorite newsgroups from home or on the road
          -----------------

          Comment

          Working...