Skip to content
Related Articles

Related Articles

Method Class | isDefault() Method in Java
  • Last Updated : 05 Dec, 2018

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();
    }
}
Output:
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();
        }
    }
}
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 Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :