Skip to content

Tag: generics

Using Generics for return type parameter Java

I am trying to create a method that will return a generic type parameter. I have a class VehicleOrder that extends the abstract class Order. In class Order, I created an abstract method receiveHiredObject. This method will not receive any parameter and will return a generic. I implemented this method in the c…

ClassCastException on raw functional interface lambda invocation

Consider this example: The last line will give java.lang.ClassCastException: class java.lang. Object cannot be cast to class java.lang.String. This is not a surprise for me, as Function is declared to accept String. I know that raw types can give ClassCastException, but all examples I saw are about ClassCastE…

Generics code not working when i try to return concrete object

The goal i am trying to achieve, is to have different FileLoaders like CSVFileLoader, ExcelFileLoader that can load up any object of type T, as long as it know how to convert using ‘C’ and create the object of type T. Hope this makes sense. I am trying to use generics to create a generic FileLoade…