• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

java 8 interface example

Interface changes in java 8

April 3, 2017 By j2eereference Leave a Comment

Interface changes in Java 8

One of the biggest design changes in Java 8 is with the concept of interfaces. Prior to version 8, Java supported only method declarations in interfaces. But from Java 8, we can have default methods in the interfaces,which can have implementation as well.

Let’s take an example : The below code will compile in Java 8.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface java8Interface {
default public void display(){
System.out.println("Inside [java8Interface] [display] ");
}
}
 
 
public class Java8InterfaceImpl implements java8Interface {
public void draw(){
System.out.println("[Java8InterfaceImpl][draw]");
}
}
 
 
public class TestInterface {
public static void main(String[] args) {
Java8InterfaceImpl interfaceImpl= new Java8InterfaceImpl();
interfaceImpl.display();
}
}

Output:

Inside [java8Interface] [display]

Why Defaut Method?

Modifying an interface in any application framework breaks all classes that implements the interface . For example adding any new method in an existing interface could break lots of lines of code. Default methods can be added to an interface without affecting implementing classes as it includes an implementation. If each added method in an interface defined with implementation then no implementing class is affected.  An implementation class can also override the default implementation provided by the interface. Default methods will help us in extending interfaces without having the fear of breaking implementation classes.

Default Method and ambiguity Problem

Lets understand the ambiguity issue with the below example code.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public interface Java8InterfaceA {
default public void display(){
System.out.println("Inside [java8InterfaceA] [display] ");
}
}
 
public interface Java8InterfaceB {
default public void display(){
System.out.println("Inside [java8InterfaceB] [display] ");
}
}
 
public class Java8InterfaceImpl implements Java8InterfaceA,Java8InterfaceB {
}

As mentioned in the above code,  if we try to implement two interfaces which are having same methods , we will get compilation error as below.

Duplicate default methods named display with the parameters () and () are inherited from the types Java8InterfaceB and  Java8InterfaceA

In order to fix this error we need to provide default method implementation:

Java
1
2
3
4
5
public class Java8InterfaceImpl implements Java8InterfaceA,Java8InterfaceB {
@Override
public void display() {
}
}

Further, if we want to invoke default implementation provided by any of super interfaces rather than your own implementation, we can do so as follows,

Java
1
2
3
4
5
6
public class Java8InterfaceImpl implements Java8InterfaceA,Java8InterfaceB {
@Override
public void display() {
Java8InterfaceA.super.display();
}
}

 

Related Posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof

Filed Under: Core Java Tagged With: default method example, java 8 interface default method example, java 8 interface example

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.