java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

DailyFrankPeter

I want to type class members, as well as args for methods, in a type T, which has to be a Node and implement my own interface Linkable.

Essentially, I would like to limit the vars to the small section of the inheritance 'tree' between Node and Linkable. Reason being I don't want to have to do all the casting inside the class, depending if I need to call methods of Node or methods of Linkable.

At the moment in my project I know I'm using only Nodes which implement Linkable, but this condition is not enforced in any way.

Pseudo-code below (only for illustration):

public class MyClass <T extends ??> {

T node1;
Bounds bounds1;

bounds1 = node1.boundsInParentProperty(); //Node specific method

node1.linkableSpecificMethod(); //method defined in Linkable interface
//....
}
Christian Hujer

There are two things that help you:

  • Generics do not distinguish between Interfaces and Classes.
  • Generics support ampersand for listing multiple types.

That said, what you probably want is this:

class YourClass<T extends Node & Linkable> {
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

From Dev

java: Which one to use as reference type when a class extends and also implements

From Dev

How to Differentiate between kotlin's class inheritence(extends in java) and interface implementation(implements in ) here kotlin uses ( : ) for both?

From Dev

Declare an attribute that both extends a class and implements an interface

From Dev

How to extends an abstract class with generics?

From Dev

Java Generics compile error in line class HanoiStack<T extends Comparable<T>> extends Stack<T>

From Dev

how to create generic java class that extends class and implements interface?

From Dev

java generics T extends Simpletype?

From Dev

Discriminator Java Jpa Class which extends can be type inherenced class

From Dev

How to turn a Ceylon class which extends a Java class into the Java class

From Dev

How to find out if a type implements generics base class

From Dev

How to find out if a type implements generics base class

From Dev

Is there a way in Java Generics to say "T extends Thing or T extends SomethingElse"?

From Dev

Is there a reason to double parametrize generics in Java?

From Dev

TypeScript: Class generics - something like Java's Class<T extends Foo>

From Dev

Generics wildcarding with both "extends" and "super"

From Dev

Inline class which extends a type parameter

From Dev

How to verify a generic type extends another class/interface in Java?

From Dev

Java: Issue combining Generics, Inner class, and "implements"

From Dev

Java Generics: How to specify a Class type for a generic typed class?

From Dev

Java Generics: How to specify a Class type for a generic typed class?

From Dev

Java extends class with inner class type

From Dev

How can we define a class which extends the Exception class of java.lang class

From Dev

Java generics extends syntax

From Dev

how to redirect from Normal java class to other java class which extends activity in android?

From Dev

Why generic type is not applicable for argument extends super class for both?

From Dev

how to use getBaseContext() in class which is not extends Activity

From Dev

How to instantiate trait which extends class with constructor

From Dev

How to save state on a class which extends ImageView?

Related Related

  1. 1

    java, generics - how to parametrize a class with a type T which BOTH: extends and implements sth

  2. 2

    java: Which one to use as reference type when a class extends and also implements

  3. 3

    How to Differentiate between kotlin's class inheritence(extends in java) and interface implementation(implements in ) here kotlin uses ( : ) for both?

  4. 4

    Declare an attribute that both extends a class and implements an interface

  5. 5

    How to extends an abstract class with generics?

  6. 6

    Java Generics compile error in line class HanoiStack<T extends Comparable<T>> extends Stack<T>

  7. 7

    how to create generic java class that extends class and implements interface?

  8. 8

    java generics T extends Simpletype?

  9. 9

    Discriminator Java Jpa Class which extends can be type inherenced class

  10. 10

    How to turn a Ceylon class which extends a Java class into the Java class

  11. 11

    How to find out if a type implements generics base class

  12. 12

    How to find out if a type implements generics base class

  13. 13

    Is there a way in Java Generics to say "T extends Thing or T extends SomethingElse"?

  14. 14

    Is there a reason to double parametrize generics in Java?

  15. 15

    TypeScript: Class generics - something like Java's Class<T extends Foo>

  16. 16

    Generics wildcarding with both "extends" and "super"

  17. 17

    Inline class which extends a type parameter

  18. 18

    How to verify a generic type extends another class/interface in Java?

  19. 19

    Java: Issue combining Generics, Inner class, and "implements"

  20. 20

    Java Generics: How to specify a Class type for a generic typed class?

  21. 21

    Java Generics: How to specify a Class type for a generic typed class?

  22. 22

    Java extends class with inner class type

  23. 23

    How can we define a class which extends the Exception class of java.lang class

  24. 24

    Java generics extends syntax

  25. 25

    how to redirect from Normal java class to other java class which extends activity in android?

  26. 26

    Why generic type is not applicable for argument extends super class for both?

  27. 27

    how to use getBaseContext() in class which is not extends Activity

  28. 28

    How to instantiate trait which extends class with constructor

  29. 29

    How to save state on a class which extends ImageView?

HotTag

Archive