• Skip to primary navigation
  • Skip to 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

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

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?

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

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

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

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

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.