Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading