Skip to content
Advertisement

Tag: generics

How to define a general interface for several type-specific classes?

While trying to program to interfaces, I regularly find myself in the following situation: I have several very similar classes representing containers or algorithms for different types. I would like to define a common interface for these classes. Consider, e.g., a string container. Such a container will likely have string processing methods. Since those methods are easily represented using generic

Unable to add value to a java generic list

Here’s my sample code: I think I should be able to add integer to items, but compilation fails: Anyone knows what’s wrong here? Answer Let us consider a more complete version of your example: Suppose for the sake of argument that the compiler says that is OK. And now we will create an instance and call the func method: Here

Java Method that returns different types of generic Lists

I’m currently trying to write a method that goes through a list of Ant-Objects and returns a list of AntScouts, that extend Ant. In general, List<Ant> can contain a lot of different Objects that inherit from Ant. I also have an enum for the different kinds of ants: This enum causes a warning: And this is the method that currently

Not able to create generic bounded class objects with interface

I am trying to use bounded types with generics to create generic objects of subclasses (these implement an interface). But I am getting type mismatch errors when initializing objects with the subclasses. Here is the interface: Here’s the class that implements this interface: Now I created 2 classes (SegmentPageScanResult and ItemProcessor) with the bounded generic type as: and When I

How rewrite Java generec’s into Kotlin?

I have java class and I need to rewrite it in kotlin. But I can’t rewrite generics into it. Java class: My Kotlin class Sign (1) I marked the my problem area by my opinion. But maybe the problem is somewhere else. Answer Kotlin’s <out FlagFilter> is not equivalent to Java’s <T extends FlagFilter>. Using out at a generic declaration

Advertisement