1

I'm learning java and Abstract Data Types and I'm getting a little bit confused.

Basically, from what I understood, Abstract Data Types are something that is defined, but not implemented. So we have the API of the Abstract Data Type and we need to implement those methods specified in the API. For example, we have a Stack and we can implement it by using arrays, nodes, etc... As an example, to declare those objects we do:

Stack ex = new ArrayStack<>(); or Stack ex = new NodeStack<>();

My problem is, we can also do Stack ex = new Stack<>(); without implementing anything. In this case which implementation are we using? What's the point of implementing it then? Can we choose which implementation we are going to use without implementing it ourselves? I'm so lost.

Another example, why do we need to create nodes to implement ADTs if we can use a LinkedList directly? Is it for leaning purposes only or are they something different?

Thanks for the help!

4492121
  • 23
  • 4
  • 1
    As sidenote, maybe this will help explain Polymorphism ("extend") vs interface: https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism – Sorin Mar 08 '21 at 20:52
  • Thanks, I'll give it a look! – 4492121 Mar 08 '21 at 21:14

1 Answers1

1

You're correct in your description of an abstract data type. However if you are referring to java.util.Stack<E>, it isn't abstract and you can check the documentation here.


Edit

A LinkedList<E> is also not abstract, but is an implementation of the List<E> interface. The Node<E> class is just used in the LinkedList<E> implementation. Apart from for learning purposes or using a Node style structure elsewhere, there is no need to create your own version.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
  • My confusion is, if it is not abstract how do we know what implementation does it use to create the stack? Isn't there a perfect implementation for each scenario? Don't we have to choose an implementation according to the scenario? – 4492121 Mar 08 '21 at 20:28
  • 1
    There is only one implementation of the particular `java.util.Stack` class? Sorry I am not sure I quite understand your question – Henry Twist Mar 08 '21 at 20:36
  • It's ok, thanks anyway for trying to help. So basically the java.util.Stack is not the implementation of the Stack API? – 4492121 Mar 08 '21 at 20:40
  • 1
    What do you mean by the Stack API? – Henry Twist Mar 08 '21 at 20:41
  • Nevermind, I think I got it. I thought the stack was an ADT and java.util.Stack was one of his implementations(?). Thank you very much! – 4492121 Mar 08 '21 at 20:46
  • 1
    To be frank, `java.util.Stack` is a terrible example for figuring out ADTs vs implementations since it's inheritance-run-wild and there isn't a separate interface. A better example in the standard library is `java.util.Map` (the ADT) vs `java.util.HashMap` (an implementation). – Ken Wayne VanderLinde Mar 08 '21 at 20:47
  • 1
    Ah no, not the case. I would recommend following the advice of @KenWayneVanderLinde though, either with `Map` and `HashMap` or `List` and `ArrayList`. – Henry Twist Mar 08 '21 at 20:49
  • 1
    Edited my answer! – Henry Twist Mar 08 '21 at 21:03
  • 1
    Thanks for the time spent! Have a great day – 4492121 Mar 08 '21 at 21:12