Home > Blockchain >  java and interfaces implementing different parameter types
java and interfaces implementing different parameter types

Time:12-02

I'm working on a basic math interface and wondering if there's a way I can specify that the type parameter can only be an integer or float without using the type?

for example, this is the basic layout, where is is where I'm trying to say Integer or Float without hard coding it into the actual abstract method.

Thanks enter image description here

CodePudding user response:

There is no way to restrict the type of a generic to a specific few but you could just require a float since floats will behave like integers

CodePudding user response:

Using <T extends Number> can restrict this to Number or its subclasses. You can check https://docs.oracle.com/javase/tutorial/java/generics/bounded.html

  • Related