Skip to content
Advertisement

Tag: generics

How to capture and propagate a wildcard type argument?

I have a couple of classes having identical methods, except with respect to certain parameter types: Now I’m trying to have a main class that stores a mixed list of these class objects and for each of these instances, passes info between its two methods: The List<ICls<?>> and Map<String, ?> statements are OK. However, the map.get(key) throws an IDE error:

ClassCastException “Parent cannot be cast to class…are in unnamed module of loader ‘app’ ” with Java Generics and inheritance

I´m currently facing a problem with generics in Java. I need to return a parent instance casted to the child. The sample below shows what I´m trying to achieve. This code does not run through and produces this exception instead: class com.generics.GenericTest$Parent cannot be cast to class com.generics.GenericTest$ChildEntity (com.generics.GenericTest$Parent and com.generics.GenericTest$ChildEntity are in unnamed module of loader ‘app’) java.lang.ClassCastException: class

How to use one type in method?

I have two forms, one form to create user and one to update user.These forms are similiar and I want to use one type in my method,I wouldn’t like to create two methods one to create user from registerForm and second to create user from updateForm Answer Use generic method like this: Output:

Advertisement