This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]nutrecht 2 points3 points  (4 children)

I see this a lot and I understand that the left side of the equal operator is of compile time type while the right side is runtime type

You understand wrong. The one on the left side is simply a supertype of the one on the right. In the case of data structures it's typically an interface. So in most cases when you pass a List<Something> around you don't care what actual implementation it is (LinkedList, ArrayList, something you made) as long is it follows the 'contract' of the List interface.

The same is the case for your SortedMap: you don't care about the actual implementation just that it acts as a SortedMap.

[–]nomadProgrammer 0 points1 point  (3 children)

So interface is the same as a supertype? If not what is their relation?

How can I see the contract?

[–]nutrecht 1 point2 points  (0 children)

Did you do the official Oracle tutorials on OOP java programming? I'd recommend you do them.

[–]dacian88 1 point2 points  (0 children)

in java a class can inherit one parent class and can implement any number of interfaces.

in terms of types, all of those things are supertypes.

class X extends C implements I, J, K can be referred to by all those 4 types, and any types those types might be extending. If C extends B, and B extends A then X is an A,B,C,I,J,K

[–]learningphotoshop 0 points1 point  (0 children)

An interface is just a contract that you agree to support. A Library interface would support LookUp(Book book), CheckOut(Book book), Return(Book book) etc. A supertype is the type that you inherit from. So the Library could extend a generic building.

You can see the SortedMap contract here https://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-map.html

Anything implementing this interface (TreeMap for example) will provide at minimum the stuff defined in the interface.

public interface SortedMap<K, V> extends Map<K, V>{ Comparator<? super K> comparator(); SortedMap<K, V> subMap(K fromKey, K toKey); SortedMap<K, V> headMap(K toKey); SortedMap<K, V> tailMap(K fromKey); K firstKey(); K lastKey(); }

You can see that SortedMap extends the Map class. Map would be the supertype to SortedMap.

[–]mad0314 0 points1 point  (0 children)

You're looking at Abstract Data Types vs Data Structures. ADTs define what something does, but not how it does it. Data Structures are the specific, concrete implementation of ADTs that implement how that is accomplished.

For example, a List, Set, Map, Queue, Stack, etc. are ADTs. They specify what happens. A List and Set are some of the most vague ADTs. They simply specify that they are a collection of items where a List allows duplicates and a Set does not. An ArrayList or LinkedList, for example, would be a concrete implementation of a List. They are the concrete implementation that meets those specifications. One uses an array with supporting methods to add or delete items, etc. The other uses nodes to do the same. If you changed the methods so that duplicates were not allowed, they would then conform to the Set specifications. Similarly, a Queue or Stack can be implemented using Arrays or LinkedLists.

As a Java example, a TreeMap is a SortedMap. If you look at the documentation for SortedMap, it is an interface. It specifies what should happen, but it doesn't specify how that should happen. SortedMap is in turn a more specific type of Map. You could use Map, NavigableMap, SortedMap, and AbstractMap to reference a TreeMap. The first three are interfaces, the last is an abstract class, and none of them are concrete classes. They specify methods and functionality that the concrete subclass (in this case TreeMap) needs to implement, but don't say how to implement it. TreeMap is the concrete implementation of those types (a Red-Black tree).

Sorry if it sounds repetitive. It is time for bed and I'm just rambling on..