Core Java

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

Soroosh Sarabadani

A worm who loves to have JAVA and share his findings with others.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button