java map getting incompatible types with child class of an abstract class

java generics
java type erasure
generic method java
java get class of generic type
java generics extends
generic list in java
arraylist generics java
t in java example

I'm trying to create a map like this

Map<String, Class<abstractClass>> map = HashMap<String, Class<abstractClass>>();

For its input, I tried

map.put("exampleString", childClass.class);

But this doesn't work, the IDE says wrong 2nd argument type.

Found: java.lang.Class<childClass>, required java.lang.Class<abstractClass>

Even though there's an inheritance relationship between them. Does anyone know how to solve this problem?

Thanks in advance!

You need a generic wildcard:

Class<? extends abstractClass>

This is because generic parameters by default are invariant. You could only put abstractClass.class in the map before the change.

By adding <? extends abstractClass>, you are making the generic parameter contravariant. In other situations, you can change extends to super to achieve covariance. There is an acronym for remembering when to use extends and when to use super, called PECS.

Time To Really Learn Generics: A Java 8 Perspective, Java introduced generic methods and classes back in 2004 with J2SE 1.5. learned the minimum they needed to know about them to get the job done. The emptyMap method uses K and V for key and value types in a generic map. String is a subclass of Object , so you can assign a String to an Object  Chapter 6. Relationships Among Classes So far, we know how to create a Java class and to create objects, which are instances of a class. But an object by itself … - Selection from Learning Java [Book]

It looks to me that the childClass.class does not extend / implement the abstract class.

Please show the childClass signature.

Java Fundamentals Tutorial: Java Collections and Generics, The Java Collections Framework is a set of classes and interfaces implementing Abstract implementations: building blocks for custom implementations Primitive types (e.g., int ) must be boxed (e.g., Integer) for inclusion in a collection. with TreeMap to get them sorted by name Map wordMap = new HashMap(); if  The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

Are you sure you don't just want:

Map<String, abstractClass> map = HashMap<String, abstractClass>();
map.put("exampleString", childClass);

?

What you have is you are taking the actual meta signature of the class and putting it into a map, instead of the actual reference to a class

Generics and Overcoming Type Erasure on the JVM, See the limitations of using parameterized types on the JVM, most importantly Generics is the direct Java implementation of template classes. Object object = objects.get(0); String string = (String) object; // Explicit casting required for sure whether any element of the list is of type T or a subclass of T. So you just get the less specialized type Response as result of foo2, which doesn't work with foo3. Note that this automatic deduction is necessary because the type parameters RS are independent for every method. Nobody guarantees that they refer to the same type in the whole class.

Actually I solved this problem by changing

Class<abstractClass> 

into

Class<? extends abstractClass>

Java Software Errors: How to Avoid 50 Code Issues in Java, This Java software error doesn't get noticed by the compiler until further in the code. public abstract class NFLPlayersReference { private static Runningback[] test.java:78: error: incompatible types return stringBuilder. public String[] OpenFile() throws IOException { Map<String, Double> map = new  So, downcasting from a base to a derived class is not possible because data members of the inherited class are not allocated. But if we instantiate MyBaseClass as MyDerivedClass the cast is allowed – in other words downcasting is allowed only when the object to be cast is of the same type as the type it’s being cast to:

Java generics and type erasure, Generics have been available in Java since J2SE 5.0 was released in 2004. not allow this to compile — the second line causes an 'incompatible types' error. a generic class with a method to create an array of the parameterised type from the vector of strings Object anObject = strings.get(0); // fetch a string from the​  Subclassing and Inheritance Classes in Java exist in a hierarchy. A class in Java can be declared as a subclass of another class using the extends keyword. A subclass inherits … - Selection from Learning Java, 4th Edition [Book]

Java Programming Tutorial on Generics, The class or method designers can be generic about types in the definition, The primary usage of generics is to abstract over types for the Collection Framework. Collection<? extends E> c) public E get(int index) public E remove​(int index) . error: incompatible types: int cannot be converted to String // JDK 7 introduces  Any object reference can be assigned to a reference variable of the type Object, because the Object class is a superclass of every Java class. There can be 2 casting java scenarios · Upcasting · Downcasting. When we cast a reference along the class hierarchy in a direction from the root class towards the children or subclasses, it is a downcast.

java.util (Java Platform SE 7 ), This class provides a skeletal implementation of the Map interface, to minimize ListResourceBundle is an abstract subclass of ResourceBundle that manages  @MartinMaat what kind of context are you missing? "No class to feature the events" what? the Event class features the events. "no subscriptions" subscription is done after the event types are added in the constructor.

Comments
  • Unrelated: please follow Java naming conventions. Class names go UpperCase always.
  • public class childClass extends abstractClass
  • I want to get the class type I would use with different input of strings, instead of objects. So I think I have to use Class<> and .class..?
  • With this implementation, I get expression expected from the childClass part.
  • Try this: Class<? extends abstractClass>