Skip to content

Instantly share code, notes, and snippets.

@BT-ICD
Created May 7, 2021 11:48
Show Gist options
  • Save BT-ICD/2470ffc16b303c9fe80314d55431d6f7 to your computer and use it in GitHub Desktop.
Save BT-ICD/2470ffc16b303c9fe80314d55431d6f7 to your computer and use it in GitHub Desktop.
Example: Generic Method with upper bound
/**
* Example: Generic Method with upper bound
* The type parameter section specifies that T extends Comparable< T >only objects of classes that implement interface Comparable< T > can be used with this method.
* In this case, Comparable is known as the upper bound of the type parameter.
* By default, Object is the upper bound.
* */
package GenericDemo;
public class GenericMethodUpperBoundDemo1 {
public static void main(String[] args) {
int ans;
ans = maximum(40,125,60);
System.out.println("Maximum is " + ans);
}
public static <T extends Comparable<T> > T maximum(T a, T b , T c){
T max =a;
if(b.compareTo(max)>0){
max=b;
}
if(c.compareTo(max)>0){
max =c;
}
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment