-4

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?

2 Answers2

1

In B interface in method foo() you can call A.foo() such way:

    @Override
    default void foo() {
        System.out.println("B");
        A.super.foo();
    }
Cookie
  • 193
  • 7
  • Ok, this example is not related to my problem. I will update the example, ```A.foo()``` is not a default method in my case. – Mateusz Maciejko Feb 08 '22 at 07:54
  • Question still is not clear. What are you want to call from `A` interface if you don't have realization there? – Cookie Feb 08 '22 at 08:00
  • Added an update section. – Mateusz Maciejko Feb 08 '22 at 08:22
  • Its not good idea to override such methods. You can see this question https://stackoverflow.com/questions/13036159/spring-data-override-save-method – Cookie Feb 08 '22 at 08:30
  • Ok, other solution is to add a method ```FooRepository.mySave()``` and ```doSth()``` in hete ,then just call ```save()``` but i want to avaid further mistakes, by someone calling just ```FooRepository.save()```, which is not recommended. – Mateusz Maciejko Feb 08 '22 at 08:38
1

I don't think you can call that method without implementing interface B. JPA creates a framework-made implementation of JpaRepository interface under the hood, an implementation that you cannot access in order to call the foo method.

AlexAlbu
  • 76
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 08:28