About Lukas Eder

Lukas is a Java and SQL enthusiast developer. He created the Data Geekery GmbH. He is the creator of jOOQ, a comprehensive SQL library for Java, and he is blogging mostly about these three topics: Java, SQL and jOOQ.

Java 8 virtual extension methods

I’ve been following the evolution of the Java 8 Lambda expressions project for a while now, and I’m really thrilled by its current state of progress. The latest “easy-to-understand” presentation I’ve found is this one:
http://blogs.oracle.com/briangoetz/resource/devoxx-lang-lib-vm-co-evol.pdf

Now, as an API designer, I’m particularly interested in the concept of virtual extension methods and I was wondering whether it was also considered to introduce “final” extension methods as opposed to “default” ones. For example:

interface A {

  void a();

  void b() default { System.out.println("b"); };

  void c() final { System.out.println("c"); };

}

When implementing the above interface A, one…

  • MUST also implement a()
  • MAY implement / override b()
  • CANNOT override c()

Advantages:

  • API designers can create convenience methods more easily without risking client code “illegally” overriding default implementations. That’s one of the main purposes of “final”.
  • Lambda expressions wouldn’t need to be limited to pure “functional interfaces” (single-method interfaces), as a functional interface would still be “functional” if it also had any number of final extension methods. For example, the above interface A would become a functional interface, if b() was removed or if b() was made final as well.
  • Extension methods would have more features in common with regular methods, which can be final as well. I guess for the reflection API and for the JVM, that’s a plus.
  • The JVM is modified anyway to support extension methods. Java 8?s momentum could be used for this feature as well, i.e. now is the right time to think about this

Disadvantages:

  • A class could inherit multiple colliding final method implementations in the case of “diamond interface inheritance“. This could lead to new compilation errors in existing code. I guess this lack of backwards compatibility is the biggest drawback.

As with multiple inheritance itself, careful API designers could further improve their API’s when using final extension methods, whereas less careful API designers might break client code. But this
is the case with previous usage of “final” as well, so I think final extension methods would be a very nice addition to Java 8.

See the full mail and follow-up on the lambda-dev mailing list here:

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-December/004426.html

Reference: Java 8 virtual extension methods from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog.

Related Articles :

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You'll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You'll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


2 × eight =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books