JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Virtual Extension Methods(or Defender Methods) in Java 8

May 16, 2012 by Mohamed Sanaulla Leave a Comment

As part of the JSR-335 (Project Lambda) which adds closure support to Java language, there were quite a few changes in the language to support the use of closures in the existing Java APIs (Collection APIs to a large extent). One such change is the introduction of Virtual Extension Methods.

also read:

  • Java 8.0 Tutorials
  • Java 7.0 Tutorials
  • New Features in Java 7.0
  • G1 Garbage Collector in Java 7.0

We all are aware of the fact that the interfaces don’t contain any implementation for the methods. To provide support for new APIs which support the use of closures and also which can run on Multi core platforms there has to be some way to add these APIs to existing classes. For example, methods like forEach, map, reduce, filter which act on a collection can be added to the Collection class directly or create a new interface and let all the collection API implement them or leave it to the user of the API to implement the new interface. The first 2 approaches would lead to breaking lots of existing code because of the lack of implementation of these new methods in the interface. The last approach is possible, but it doesn’t enhance the collection API out of the box.

The team which handles JSR-335 thought of a way to add default implementation to the interfaces, which gives an option for the implementor to override the method or to leave it as is. This way new APIs can be added to the Collection class without breaking the existing code and yet provide the full support of the closures to the existing code. One can read in depth about Virtual Extension Methods here.

Lets see an example of a virtual extension method:
[code lang=”java”]
interface TestInterface{
public void testMe();

public void aDefaulter() default{
System.out.println("Default from interface");
}
}
[/code]
Using the “default” keyword we can add new methods to the existing interface and allowing the implementing classes to use this default implementation if they don’t override. Something like:
[code lang=”java”]
public class DefenderMethods{
public static void main(String[] args){
InterfaceImplementer imp = new InterfaceImplementer();
imp.testMe();
imp.aDefaulter();
}
}
class InterfaceImplementer implements TestInterface{
public void testMe(){
System.out.println("Hello World!");
}
}
[/code]

The output for above program would be:
[shell]
~/javaP/java8$ javac DefenderMethods.java
~/javaP/java8$ java DefenderMethods
Hello World!
Default from interface
[/shell]

Moving further, we can override the aDefaulter method in its implementing class, something like
[code lang=”java”]
class InterfaceImplementer implements TestInterface{
public void testMe(){
System.out.println("Hello World!");
}

public void aDefaulter(){
System.out.println("Defautler overridden from class");
}
}
[/code]
for which the output will be:
[shell]
~/javaP/java8$ javac DefenderMethods.java
~/javaP/java8$ java DefenderMethods
Hello World!
Defautler overridden from class
[/shell]

The bytecode generated for the DefenderMethods class still uses invokevirtual opcode for invoking the default method, which indicates its treated like any other usual interface method in its invocation.

There’s lot more to this concept like overriding rules, method invocation rules which I would like to cover in subsequent posts. And also sample code from the JDK 8 which uses this feature.

Filed Under: Java Tagged With: Java, Java 8, Project Lambda

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved