Skip to main content

Java 8 features ,Default Methods and Static Methods

 Java as an Object oriented programming language was invented in the early 90s.

In year 2005 , added major changes such as :

1.Enhanced for loops

2.Generics

3.Type-safe enumerations.

4.Annotations.

again after 10 years , in 2015 java 8 was introduced with lot of new features :

such as :

1.Lambda Expressions

2.Method References

3.Default methods

4.New Stream API

5.New Date/Time API

6.Nashorn

-> now in java 8 ,interface introduced with some new changes.

interface can now define static methods.

interface can now define default methods.

package com.suv.java8;
public class Java8Demo implements Calculator {
	public static void main(String[] args) {
		System.out.println("Welcome to https://www.crtr4u.com/");
		Java8Demo jd = new Java8Demo();
		jd.msgDisplay();
		Calculator.sayHi();
	}
}
interface Calculator {
	default void msgDisplay() {
		System.out.println("this is default method introduced in java 8 interface");
	}
	static void sayHi() {
		System.out.println("Hi, this is static method allowed in interface from java 8");
	}
}

Output
Welcome to https://www.crtr4u.com/
this is default method introduced in java 8 interface
Hi, this is static method allowed in interface from java 8

-One Problem we must understand regarding default method in java 8 :
In Java , Multiple inheritance is not possible using classes
for example : class A extends B,C {} is not allowed in java 
But , we can implement multiple interface
for example : class A implements InterfaceB , InterfaceC{}
Now problem is , what if both interface contain default method with same signature , look at below snapshot :


As shown , In above snapshot our both interfaces contain same method with same signature , now when AClass is implementing both interface InterfaceB and InterfaceC , 
we are getting error as :

Duplicate default methods named displayMsg with the parameters () and () are inherited from the types InterfaceC and InterfaceB

we can resolve above error as :
Way 1 : Overriding default method implementing class 


Way 2 : Invoke the default method test from parent interface

-forEach() method in java 8: 

For handling Collections in better way ,forEach method is introduced ,
forEach() is the default method of Iterable interface of java.lang package.
Example : 
class Employee{
	int empId;
	String empName;
	public Employee(int empId, String empName) {
		super();
		this.empId = empId;
		this.empName = empName;
	}
	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName + "]";
	}	
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ForEachDemo {
 public static void main(String[] args) {
	List<Employee> empList=new ArrayList<Employee>();
	empList.addAll(Arrays.asList(			
		new Employee(1,"Neel"),
		new Employee(2,"Prashant"),
                new Employee(3,"Jay"),
		new Employee(4,"Jeet"),
                new Employee(5,"RK")
	));
	empList.forEach(e->System.out.println(e));
	}
}

Output :
Employee [empId=1, empName=Neel]
Employee [empId=2, empName=Prashant]
Employee [empId=3, empName=Jay]
Employee [empId=4, empName=Jeet]
Employee [empId=5, empName=RK]

Happy Learning,thanks for reading our article.

Popular Posts

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

How much Software Engineer Earn in India

  No limit of earning for free lancer software developer : In service based companies - If Software Engineer work for same MNC from beginning of career then sometimes financial growth is low,still they can reach in between 7 lacs to 15 lacs in India. If programmer switch job after every 2-3 year and work for at least 5 years then they can earn minimum in between 14 lacs to 25 lacs for the position of Senior Software Engineer.  Being from a normal college , completed my education with average percentage , then  As 2 year experienced guy - I was getting offers of 8 lacs, 10 lacs  As 5 years experienced guy the offers are like  Minimum 12 lacs ,14 lacs, 18 lacs from Indian MNCs.  Money is  no bar for right candidate, if you are able to crack interview and if you can  write effective code then you can earn more than 20 lacs in India itself. Be motivated, improve your technical skills and yes you can earn great amount from Job too...  People with ...