Created
May 7, 2021 11:48
-
-
Save BT-ICD/2470ffc16b303c9fe80314d55431d6f7 to your computer and use it in GitHub Desktop.
Example: Generic Method with upper bound
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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