Questions tagged [multiple-bounds]

Also known as "type intersection", when the generic type is bounded by multiple types, eg

In Java, a generic type may be upper bound to more than one type, using the & operator, as follows:

<T extends B1 & B2 & B3>

At most 1 of the bounds may be a class.
If a class is present in the list, it must be first.

See the relevant Oracle tutorial page for more info.

4 questions
25
votes
2 answers

How does the Java compiler choose the runtime type for a parameterized type with multiple bounds?

I would like to understand better what happens when the Java compiler encounters a call to a method like the one below. void printType(T... args) { …
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
5
votes
1 answer

When to use or how to use Multiple Bound in generics

I am new to generics and learning generics from hear https://docs.oracle.com/javase/tutorial/java/generics/bounded.html I am learning about Multiple Bounds what I understood is you can specify class like follows class D { /*…
Miral Bhalani
  • 274
  • 2
  • 9
4
votes
1 answer

Static field with generic parameter extending multiple types

What I have without any problem: I have multiple enums which extend the same interface: interface CommonMethods { // some methods } enum E1 implements CommonMethods { O1, O2; // interface method impls } enum E2 implements CommonMethods…
Bartis Áron
  • 618
  • 1
  • 10
  • 22
2
votes
1 answer

How to use multiple bounds correctly?

Im trying to create an instantiate a generic class called "MultipleBoundsClass" that has multiple bounds - a class called "OrderedPair" and an interface called "Pair"(which Ordered Pair implements). Ive tried removing the interface boundary and that…
Dylan
  • 375
  • 1
  • 2
  • 12