What are the negative aspects of Java class Stack inheriting from Vector?

前端 未结 6 847
太阳男子
太阳男子 2020-11-30 04:35

By extending class Vector, Java’s designers were able to create class Stack quickly. What are the negative aspects of this use of inheritance, particularly for class Stack?<

6条回答
  •  孤街浪徒
    2020-11-30 04:39

    It violates the very first rule we all learned about inheritance: can you, with a straight face, say that a Stack IS-A Vector? Clearly not.

    Another more logical operation would be to have used aggregation, but the best option IMO would be to have made Stack an interface which could be implemented by any appropriate data structure, similar (but not exactly the same) to what the C++ STL does.

提交回复
热议问题