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:
import java.lang.reflect.Method;
public class GFG {
public static void main(String args[])
{
try {
Class c = Shape. class ;
Method[] methods = c.getMethods();
for (Method m : methods) {
if (m.isDefault())
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:
import java.lang.reflect.Method;
import java.util.Comparator;
public class Main6 {
public static void main(String args[])
{
try {
Class c = Comparator. class ;
Method[] methods = c.getMethods();
System.out.println( "Default Methods of Comparator Interface" );
for (Method method : methods) {
if (method.isDefault())
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–
Last Updated :
05 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...