3

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.

Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33
  • of course not, since you need an implementation of the outer getRevision. if you pass an instance of an implementation of the outer interface, you can call getRevision() on that and use that – Stultuske Jan 03 '18 at 11:56
  • just define a default implementation for `getRevision` directly, you can't implement a method in a method. _INNER_ are for class implementation, not methods. EDIT : In which case should this return `getRevision() + 1` ? – AxelH Jan 03 '18 at 11:57
  • I would have used an abstract class for this instead of an interface, after java 8 you could have static classes in the interface. – Nyranith Jan 03 '18 at 12:01

4 Answers4

6

I'm not sure what's the purpose of getNextRevisionIdentifier() returning an instance of GroupIdentifier.

If you want getNextRevisionIdentifier() to return the next identifier, let it return an Integer:

public interface GroupIdentifier {

    Integer getRevision();

    default Integer getNextRevisionIdentifier() {
        return getRevision () + 1;
    }

}
Eran
  • 387,369
  • 54
  • 702
  • 768
2

Return a new instance of GroupIdentifier for which you implement getRevision accordingly:

public interface GroupIdentifier {

    //OUTER GET REVISION
    Integer getRevision();

    default GroupIdentifier getNextRevisionIdentifier() {
        //INNER GET REVISION
        Integer outer = getRevision();
        return new GroupIdentifier() {
            @Override
            public Integer getRevision() {
                return outer + 1;
            }
        }; // can be simplified to return () -> outer + 1;
    }
}

This can be run, e.g. via

public static void main(String[] args) {
    GroupIdentifier groupIdentifier = new GroupIdentifier() {
        @Override
        public Integer getRevision() {
            return 1;
        }
    };
    System.out.println(groupIdentifier.getNextRevisionIdentifier().getRevision());
}

outputting

2

luk2302
  • 55,258
  • 23
  • 97
  • 137
2

I believe what you wanted to write is this:

public interface GroupIdentifier {
    Integer getRevision();

    default GroupIdentifier getNextRevisionIdentifier() {
        return new GroupIdentifier() {
            public Integer getRevision() {
                return GroupIdentifier.this.getRevision() + 1;
            }
        };
    }
}

You just didn't find a way to refer to the enclosing instance's getRevision(). BTW if your interface is as you've posted it, with just a single abstract method, then you can implement it much more cleanly:

public interface GroupIdentifier {
    Integer getRevision();

    default GroupIdentifier getNextRevisionIdentifier() {
        return () -> getRevision() + 1;
    }
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • yes thank you very much for solution. can you explain me how it refer outer getRevision method when we use as GroupIdentifier.this.getRevision() . need to know how it works and concept behind it. thanks – Chamly Idunil Jan 04 '18 at 02:29
  • It's the Java syntax to refer to the enclosing instance. It's called "qualified `this`". Note that the type of your anonymous class is _not_ `GroupIdentifier`, it is something like `GroupIdentifier$1`. `GroupIdentifier.this` refers to the enclosing instance whose type is `GroupIdentifier`. – Marko Topolnik Jan 04 '18 at 08:54
1

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;
            }
        };
    }
}
Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33