Answers for "interfaces in java 8"

0

functional interface in java 8 example

@FunctionalInterface
interface Square
{
    int calculate(int x);
}
  
class Test
{
    public static void main(String args[])
    {
        int a = 5;
  
        // lambda expression to define the calculate method
        Square s = (int x)->x*x;
  
        // parameter passed and return type must be
        // same as defined in the prototype
        int ans = s.calculate(a);
        System.out.println(ans);
    }
}
Posted by: Guest on June-28-2021
0

How many static methods can an interface have?

package com.journaldev.java8.staticmethod;

public interface MyData {

	default void print(String str) {
		if (!isNull(str))
			System.out.println("MyData Print::" + str);
	}

	static boolean isNull(String str) {
		System.out.println("Interface Null Check");

		return str == null ? true : "".equals(str) ? true : false;
	}
}
Posted by: Guest on March-24-2020
0

How many static methods can an interface have?

package com.journaldev.java8.staticmethod;

public class MyDataImpl implements MyData {

	public boolean isNull(String str) {
		System.out.println("Impl Null Check");

		return str == null ? true : false;
	}
	
	public static void main(String args[]){
		MyDataImpl obj = new MyDataImpl();
		obj.print("");
		obj.isNull("abc");
	}
}
Posted by: Guest on March-24-2020

Browse Popular Code Answers by Language