Issue
I want to call a super method from overriden method with the same name from an interface thats extending another interface
Example:
default private interface A {
void foo() {
System.out.println("B");
}
}
private interface B extends A {
@Override
default void foo() {
System.out.println("B");
}
}
I want to call classC(implements interface B).foo()
ending with output
B
A.foo() call
How can i achive this?
In my case i dont want to implement interface B, because it's a interface thats extending JpaRepository
, and I'm just calling repository methods using only this interface.
UPDATE:
I gave the wrong example, thats not describing my problem. Below is the description of my specific problem.
Before saving object to db i want to make some actoions on this object. I'm using interface repositories. I want to override a Repository.save(Object o)
method, and make changes there, but im just not able to call a super method from extended repository.
Example:
public interface FooRepository extends MongoRepository<Foo, String> {
@Override
default <S extends Foo> S save(S s) {
s.doSth();
return MongoRepository.super.save(s); //This doesn't compile
}
}
I know i can do this by creating the class and injecting FooRepository
in there, but is there any way to do this by overriding a save()
method in FooRepository
?
Solution
In B
interface in method foo() you can call A.foo()
such way:
@Override
default void foo() {
System.out.println("B");
A.super.foo();
}
Answered By - Cookie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.