Skip to content
Advertisement

Calling an abstract method from an interface without overriding it

I am working on a Java project and want to understand the source code before implementing mine into it. The problem im facing is, how can the source code uses the abstract method from an interface without actually overriding it?

At first, I came to this statement: DeviceService deviceService = context.deviceService();

It is using ‘context’ to call the method deviceService(). Then, I went up to see where was ‘context’ being initialized. Then I found this: private final LinkDiscoveryContext context;

So, ‘context’ is a type of LinkDiscoveryContext. But then, ‘LinkDiscoveryContext’ is actually an interface. and the deviceService() is actually an abstract method without having any concrete definition of how the method should do.

I thought we cannot call the method of an interface unless we override it, but why is it working? I may be lack in developer knowledge in Java, and might have provided not enough information. But if the community has any guessing that could result in this working, please share with me.

Thanks in advance.

Advertisement

Answer

The class you mention has the constructor that takes instance of LinkDiscoveryContext as argument. So basically you assign a specific implementation to that field using class constructor.

Look for constructor usages to check where it is called from. Hence you will know from which place(s) does that implementation come.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement