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();
    }
}
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


+ 5 = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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

15,153 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