Search code examples
javainterfacedefaultdefault-method

Call Method from default method in java interface


This may be silly question. but i want to know there is some possibility to do this.

Suppose I have interface like

public interface GroupIdentifier {

    Integer getRevision();
}

And I need another method named as getNextRevision. So what i can do is, implement default method inside same interface and return next number.

EX :

public interface GroupIdentifier {

    //OUTER GET REVISION
    Integer getRevision();

    default GroupIdentifier getNextRevisionIdentifier() {
         return new GroupIdentifier() {
         //INNER GET REVISION
         public Integer getRevision() {
               //Here I want to return OUTER GET REVISION + 1
               return null;
         }
         };
    }
}

Is there some possibility to this.


Solution

  • I have fixed my solution as below.

    public interface GroupIdentifier {
        Integer getRevision();
    
        default GroupIdentifier getNextRevisionIdentifier(GroupIdentifier identifier) {
            return new GroupIdentifier() {
                @Override
                public Integer getRevision() {
                    return identifier.getRevision() + 1;
                }
            };
        }
    }