• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with stack class

 
sovan chatt
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
removeElementAt() method is available in vector class so,its also available to stack class ..but using this method any element can be eliminated..which cannot be a proper implementation of stack... or is it?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's not ideal. Those are classes from the original version of Java, and if they were added now I suspect they'd be designed differently - I'd expect to see Stack as an interface, with some class (possibly one of the existing collections) implementing it. A bit like the way LinkedList implements the Queue interface nowadays.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. This is one example of incorrect inheritance. A stack is not a vector, so Stack should not extend Vector. Similarly, a property list is not a hash table, so Properties should not extend Hashtable. In both cases, composition would have been preferable. There is one item also listed in Effective Java related to this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to use this instead of Stack.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic