I have question about the Polymorphism in Java

jfhuang :

I define class variable map

private Map<Double, Integer> map = new TreeMap<>((o1, o2)->-o1.compareTo(o2));

But when I can't call its method in class method

private void inOrder(BalancedBinaryTree.TreeNode root, double target, int k){
    ...
    map.pollFirstEntry();
    ...
}

But when I Fix it into

private TreeMap<Double, Integer> map = new TreeMap<>((o1, o2)->-o1.compareTo(o2));

Now I can call the method of TreeMap, why is that? Why the Polymorphism loses in this situation?

Olimpiu POP :

You are defining a variable of type Map using the TreeMap implementation. In this case, you will be able to call just the methods that are define in Map. In the second case, you are defining a TreeMap variable so you can call all the extra methods you mentioned.

If, in the first case you would cast to TreeMap you would be able to call the method as well.

Something like this:

import java.util.Map;
import java.util.TreeMap;

public class TreeMapCheck {
        static Map<Integer, Double> map = new TreeMap<>();
        static TreeMap<Integer,Double> treeMap = new TreeMap<>();

    public static void main(String[] args) {
        ((TreeMap)map).pollFirstEntry();
    }
}

This kind of approach would fail if there is a difference implementation than the one you would expect, so one can protect herself/himself from that checking whether the given instance is a TreeMap, like this:

public static void main(String[] args) {
    ((TreeMap)map).pollFirstEntry();

    if (map instanceof TreeMap) {
        ((TreeMap)map).pollFirstEntry();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I have a question about java polymorphism

i have question about java function signature

I have a question about percentage change in java

Hey, I have a question about assertions and mocking in java

I have a question about regexp pattern matching for java

I have a strange question about array php

I have a question about ATR Bands in FineScript

I have a theoretical question about computer organization

I have a question about the effect of a sender channel

I have a question about webrtc implementation

I have a question about onChange event in React

I have question about group by how to use it?

I have a question about c with send a signal

I have a question about logstash grok filter

i have a question about nullable variable in kotlin

I have a question about Bubble Process in Javascript

I have question about component of Github project?

I have a question about postgreSQL cluster

I have a question about react array clearing

I have a question about python (for-loop)

I have a question about printf() function

I have a question about generic view in Django's tutorial example

Hey, I have a question about dependency injection in Spring and bean instantiation

I have a question about std::vector<shared_ptr>

I have a question about cleaning up unused indexes in mongodb

I have a question about C ++ boost :: asio and std :: async

I have a question about Go pointer usage in package bytes

I have a question about Python the numerical computation of letters

I have a question about js Async Await behavior structure