Q:

How to create a map with two children extending a similar parent?

Is it possible to create a map (TreeMap) with two children? EX:
I have two classes, A and B, which both extend C. I wish to add BOTH A and B to the SAME map. Each class extends C, but I can't figure out how - obviously declaring as so:
Map<String, ? extends C> myMap = new TreeMap<String, ? extends C>();

doesn't work.
I need this because it will make my program a whole lot easier to keep both these objects in the same map. From there I can simply check:
if (map.get(key) instanceof A) {do what I want for A objects}
I also don't wish to cast them down to class C as I require the methods in class A and B respectively once they're "out" of the map.
Both objects perform a similar base function, but have very different methods and fields, so I really can't work around this without unnecessarily "combining" them and just adding extra variable to check later, i.e. (private static boolean isClassA;), so I NEED them to be separate classes, but while in the Map it doesn't matter since they both extend class C and share a common required method in it. Thank you!
MORE INFORMATION ON THE PROGRAM:
Basically class A and B are set according to a phrase read in from a database. They are set similar to this:
map.put("This is one phrase for class A", new class A());
map.put("Phrase for class B", new class B());
Now, I am parsing a paragraph input from a user. The main issue that I am comparing the string the user has entered, and then REMOVING any parts that share a key with class A or class B. Here's an example related to the above map values:
"This is one phrase for class A which will be taken out, and this Phrase for class B will also be removed." Based on this input when we loop through the map (which hopefully will contain both class A and class B) it will remove BOTH instances of from the string. By using a method to cut the string (when matches are found in the keys of the map), so the returned string will be:
"which will be taken out, and this will also be removed".
NOW, here's the main issue with having DIFFERENT maps. I am SORTING the map based on the length of the keys. So LARGER strings will be checked first, and the users sentence will be cut in order from largest strings to smallest strings.
Hopefully now you see the issue. If I have TWO maps then when I check the users input for keys containing class A it will go from ALL the largest strings in class A to the smallest strings. Which means any LARGE strings in class B (when searched through after class A has searched) will be mutilated.
For example: Class A has keys "I am" and "I feel great"
Class B has keys "I am happy" and "I feel.
given the user sentence: "I am happy and feel great.
We search with the first map containing instances of Class A. Class A (by the cut method) will remove "I am" from the sentence. But this is incorrect, since class B clearly has a longer String "I am happy" which matches the users sentence. If we had a map containing BOTH A and B then we would have a map sorted in order with keys like so:
"I feel great" (Class A)
"I am happy" (Class B)
"I feel" (Class B)
"I am" (Class A)
And so we would get the result of CLASS B trimming the sentence to: "and feel great" since Class B had a larger match with the key "I feel great".
THIS is the reason I need them in the same map, simply to sort them in ascending order and ensure they trim the users string correctly. I can't simply loop through one map, then the other :/

A:

I know that you have said that you cannot cast them down to classC, because you require some custom classA and classB method. I may be misunderstanding something, but cannot cast them to classA or classB after the instanceof. In that moment, you already know what class is, and thus can cast to A or B and then use the custom method.
Something like this (please let me know if I have lost something in your problem):
package iseji.app.main;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String args[]){
        Map<String, ClassC> myMap = new HashMap<String, ClassC>();
        ClassA classA = new ClassA();
        ClassB classB = new ClassB();

        myMap.put("1", classA);
        myMap.put("2", classB);

        ClassC classC = myMap.get("1");
        if(classC instanceof ClassA){
            System.out.println("Is A");
            classC.printClassName();
            ((ClassA) classC).myClassAMethod();
        }else{
            System.out.println("Is B");
            classC.printClassName();
            ((ClassB) classC).myClassBMethod();
        }

    }
}

