Method Class | isDefault() Method in Java
The java.lang.reflect.Method.isDefault() method is used to check whether the method of the Method object is a Default Method: or not. It returns true if method object is a default method, otherwise it will return false.
Default Method: A public non-abstract on-static method with a body declared in an interface type.
Syntax:
public boolean isDefault()
Return Value: This method returns a boolean value. It returns true if method object is a default method by JVM Specifications, else false.
Below program illustrates isDefault() method of Method class:
Example 1:
/* * Program Demonstrate isDefault() method * of Method Class. */import java.lang.reflect.Method; public class GFG { // create main method public static void main(String args[]) { try { // create class object for interface Shape Class c = Shape.class; // get list of Method object Method[] methods = c.getMethods(); // print Default Methods for (Method m : methods) { // check whether the method is Default Method or not if (m.isDefault()) // Print System.out.println("Method: " + m.getName() + " is Default Method"); } } catch (Exception e) { e.printStackTrace(); } } private interface Shape { default int draw() { return 0; } void paint(); } } |
Method: draw is Default Method
Example 2:
/* * Program Demonstrate isDefault() method * of Method Class. * This program checks all default method in Comparator interface */import java.lang.reflect.Method; import java.util.Comparator; public class Main6 { // create main method public static void main(String args[]) { try { // create class object for Interface Comparator Class c = Comparator.class; // get list of Method object Method[] methods = c.getMethods(); System.out.println("Default Methods of Comparator Interface"); for (Method method : methods) { // check whether method is Default Method or not if (method.isDefault()) // print Method name System.out.println(method.getName()); } } catch (Exception e) { e.printStackTrace(); } } } |
Default Methods of Comparator Interface reversed thenComparing thenComparing thenComparing thenComparingInt thenComparingLong thenComparingDouble
Reference:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isDefault–
Recommended Posts:
- Method Class | getParameterTypes() Method in Java
- Method Class | getGenericExceptionTypes() Method in Java
- Method Class | getParameterCount() method in Java
- Method Class | getDefaultValue() Method in Java
- Method Class | getReturnType() Method in Java
- Method Class | getDeclaredAnnotations() method in Java
- Method Class | getName() Method in Java
- Method Class | getModifiers() method in Java
- Method Class | getDeclaringClass() method in Java
- Method Class | isVarArgs() Method in Java
- Method Class | getExceptionTypes() Method in Java
- Method Class | hashCode() Method in Java
- Method Class | toGenericString() method in Java
- Method Class | equals() Method in Java
- Method Class | getTypeParameters() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



