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:

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* 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();
    }
}

chevron_right


Output:

Method: draw is Default Method

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* 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();
        }
    }
}

chevron_right


Output:

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–

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.