0

I have confusion with polymorphism (same scheme as CGLIB proxy). I have two classes:

  • ParentService -> looks like target object
  • ChildService -> looks like a proxy object
public class ParentService {

    public void method1() {
        System.out.println("ParentService.method1()");
    }

    public void method2() {
        System.out.println("ParentService.method2()");
    }

    public void callBoth() {
        System.out.println("ParentService.callBoth()");
        method1();
        method2();
    }

}

and

public class ChildService extends ParentService {

    private final ParentService parentService;

    public ChildService(ParentService parentService) {
        this.parentService = parentService;
    }

    @Override
    public void method1() {
        System.out.println("ChildService.method1()");
        parentService.method1();
    }

    @Override
    public void method2() {
        System.out.println("ChildService.method2()");
        parentService.method2();
    }

    public void callBoth() {
        System.out.println("ChildService.callBoth()");
        parentService.callBoth();
    }
}

Output:

ChildService.callBoth()
ParentService.callBoth()
ParentService.method1()
ParentService.method2()

Why in this case we don't call ChildService implementation? Please, don't mark this question unuseful, I really wasn't able to found the answer. Thank you for your patience.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 1
    Why should it? You are calling the methods on the delegate. It doesn't matter that your `ChildService` extends the `ParentService` as the call is to `parentService.callBoth()` not `super.callBoth()`. – M. Deinum Aug 17 '21 at 07:56
  • Based on the question title, you are asking something about cglib proxies. But your example code does not use cglib proxies. – Stephen C Aug 17 '21 at 07:58
  • *"Please, don't mark this question unuseful ..."* - If don't want us to do that, you will need to **EDIT** the question so that we can understand what you are actually asking. (Sorry ... but questions that only the question author can understand are ultimately unuseful.) – Stephen C Aug 17 '21 at 07:59
  • @StephenC, okay, no problem, I have edited it. But I wrote the sample example of CGLIB proxy flow. – artyom.poteshkin98 Aug 17 '21 at 08:02
  • @M.Deinum, thank you, but why does java not call the subclass? – artyom.poteshkin98 Aug 17 '21 at 08:13
  • 1
    Why should it? You are calling methods on the delegate. – M. Deinum Aug 17 '21 at 08:15
  • @M.Deinum, but why is delegate not working as typical inheritance, which limitation allows the delegate not to call subclass? – artyom.poteshkin98 Aug 17 '21 at 08:20
  • This has nothing to do with spring, proxies etc. this is basic java. There is no subclass. You are calling the methods on the delegate... – M. Deinum Aug 17 '21 at 08:21
  • @M.Deinum, okay, I understand if I write this.parentService = new ChildService(); all will be work good :) – artyom.poteshkin98 Aug 17 '21 at 08:26

0 Answers0