About Soroosh Sarabadani

My name is Soroosh Sarabadani from Iran. I'm working more than 8 year in development and design Enterprise softwares, with master of software engineering grade.

Default methods an approach to extend legacy code

As you know the new version of java was released on 18 March 2014 and i am going to introduce a serial of posts to demonstrate its new features and maybe in some points i will talk about my ideas and criticism about them.

The first feature which i think is important is “Default methods”, In all previous versions of java language the interfaces could just include method definition (declaration) not method implementation (method body), But in java 8 a new feature added to interfaces which makes you able to declare methods with their implementations in interfaces.

Suppose to this new feature you can create an interface like:

public interface Dog {
    void bark();
 
    default void bite() {
        System.out.println("Biting Biting Biting");
    }
}
 
public class Husky implements Dog {
    @Override
    public void bark() {
        System.out.println("barking");
    }
 
    public static void main(String... args){
        Dog dog = new Husky();
        dog.bite();
        dog.bark();
    }
}

It is completely self explained, You can add behaviors to your interfaces and all the implemented classes will have this behavior as the default implementation of method, So they will not be forced to implement default methods.

The reason of default method

In one of the previous posts  we had an introduction about Open Close Principal, Just as a review in this principal classes should be close for modification and open for extending. I think default methods do not follow this principal but there are some points which maybe we don’t have any solutions to extend our legacy codes.

For example in java 8 a new feature added to language which you can use lambda on collections, one of the ways you can use this is calling the stream method of Collection interface, If it was just a method declaration all the written codes which implemented Collection would be break.

Also some times it happened for me that need to  extend my interface but because many other clients were using interface i had to find another solution and unfortunately in most of the times it was a messy way.

Some points about default methods

There are some points you should know when you want to use default methods or you want to use codes which are using default methods.

    • Extending interfaces that contain default methods:
      When you want to extend or implement an interface with default methods you have three choices about default methods.

      • You can use their default implementation and ignore to redefine them.
      • You can redeclare it, So it will be an abstract method.
      • You can override it just with redefining it.
    • Multiple inheritance with default methods:With using default methods you can have classes which have a mixin behavior of many interfaces but you should notice to an important point.
      If extended interfaces have a common method signature you  will face with a compile time error regards to there is an ambiguity between two implementations of the same method signature, In this situation you will need to override the method and implement it by your own code or select one of the default methods.
public interface FirstInterface {
    default void doSomething(){
        System.out.println("Doing something from FirstInterface");
    }
 
}
 
public interface SecondInterface {
   default  void doSomething(){
       System.out.println("Doing something from SecondInterface");
   }
}
 
public class FirstImplementation implements SecondInterface,FirstInterface {
 
    @Override
    public void doSomething(){
        SecondInterface.super.doSomething();
    }
 
    public static void main(String... args){
        System.out.println("GGG");
 
        new FirstImplementation().doSomething();
    }
}

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


7 − = five



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close