When you implement an interface in Java, is it explicit or implicit?

I was just starting to figure out the difference between an implicit and an explicit interface application in .NET. As I come from the Java background, the idea is still a bit confusing. I hope that knowing what Java does will make it more obvious what the difference is. I assume Java is explicit ???

+4
source share
2 answers

No Java implicitly. Explicit is where you implement several interfaces that have the same method signatures in them, and you explicitly , for the interface for which implementation is required.

Example from MSDN:

public class SampleClass : IControl, ISurface { void IControl.Paint() { System.Console.WriteLine("IControl.Paint"); } void ISurface.Paint() { System.Console.WriteLine("ISurface.Paint"); } } 

Here we have two Paint() methods, one from each interface. In Java, you can implement one Paint (). In C #, you have the opportunity to implement versions for each interface, so you get different behavior depending on what the class is called.

So, if I called:

 SampleClass c = new SampleClass(); ((IControl)c).Paint(); ((ISurface)c).Paint(); 

I get a printout of "IControl.Paint" and then a printout of "ISurface.Paint".

+5
source

In Java, there is no difference between explicit and implicit. If you have two interfaces that declare a method with the same signature and a class that implements both interfaces, then the method in the class with the correct signature implements the method in the BOTH interfaces.

+2
source

Source: https://habr.com/ru/post/1302848/


All Articles