Java Generics Clarification( Constraining T to a type, while using Comparable) -


if create class , class intended accept specific type of data. proceed use generics , in class proceed have type constrained upper bound of number class.

so class can use types of number class or subclasses of number (integer, double, long, etc).

lets have array of these types , wish compare values in array. class contains array of these allowed data types, , wish compare these types only.

we can using comparable interface contains compareto method. can use compareto method classes implement comparable interface. number 1 of classes implement interface.

so if have class header specifies following:

public class somename<t extends number> { 

this implies constraint of type t accept types of number or subclasses of number. if wanted use compareto method comparable interface, numbers class implements, t constrained to.

if have method such as

public t somename(t[] somevariable) { 

why can't method use compareto method?t constrainted number class, implements comparable.

if have

 public <t extends comparable<t>> somename(t[] somevariable) { 

then works because t extends comparable, samething t implements comparable in generic notation... isn't practically same thing above though?

number not implement comparable.

the thing use is:

somename<t extends number & comparable<? super t>> 

at present, cannot impose restrictions on t in instance method, if write

public <t extends comparable<t>> t somename(t[] somevariable) 

that's different t.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -