default method of java 8 gives a flexibility to modify or add new upcoming feature in future on existing interface. default method does not effect to all other implementer classes if something is changed on default method, because default method is optional for exiting implementer classes to use default method even not need to override it by all implementer classes.
default method does not create ambiguity if 2 interface having same default method name. its is accessed via interface name.
public class Java8InterfaceDefaultMethod implements IConsumerUpdate ,IMarketProduct ,IMarketInventry{
public static void main(String[] args) {
// TODO Auto-generated method stub
Java8InterfaceDefaultMethod obj = new Java8InterfaceDefaultMethod();
int saleCount = obj.getProductSaleCount(10);
System.out.println("Product sale count : " + saleCount);
String pName = obj.getProductName();
System.out.println("Product Name : " + pName);
obj.getConsumer();
String pQuality=IMarketProduct.getProductQuality();
System.out.println("product quality : " +pQuality);
}
@Override
public int getProductSaleCount(int saleCount) {
// TODO Auto-generated method stub
return saleCount;
}
@Override
public int getInventryCount() {
// TODO Auto-generated method stub
return 0;
}
}
interface IMarketProduct {
String productName = "MacBook pro";
String productQuality = "Normal";
int getProductSaleCount(int count);
default String getProduct() {
return productName;
}
static String getProductQuality() {
return productQuality;
}
}
interface IMarketInventry {
String name = "Windows laptop";
int getInventryCount();
default String getProductName() {
return name;
}
}
interface IConsumerUpdate {
default void getConsumer() {
System.out.println("I am Consumer");
}
}
3. Lambda Expressions
public class ThreadEvenOdd {
int inputNum=13;
public ThreadEvenOdd(int a) {
super();
this.inputNum = a;
}
public static boolean checkPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int inputNumber=13;
ThreadEvenOdd obj = new ThreadEvenOdd(inputNumber);
/**
* Using Lambda expression introduced in java 8
*/
Runnable runnable = () -> {
// TODO Auto-generated method stub
for (int i = 0; i < obj.inputNum; i++) {
try {
if (i % 2 == 0) {
System.out.println("Even nuber is: " + i);
}
Thread.sleep(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
/**
* using java 7
*/
Runnable runnable2 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < obj.inputNum; i++) {
try {
if (i % 2 != 0) {
System.out.println("odd nuber is: " + i);
}
Thread.sleep(0);
} catch (Exception e) {
e.getMessage();
}
}
}
};
/**
* Using Lambda expression introduced in java 8
*/
Runnable runnable3 = () -> {
// TODO Auto-generated method stub
for (int i = 2; i < obj.inputNum; i++) {
if (checkPrime(i)) {
System.out.println("Prime number " + i);
}
}
};
Thread t = new Thread(runnable);
Thread t1 = new Thread(runnable2);
Thread t2 = new Thread(runnable3);
t.start();
t1.start();
t2.start();
}
}