Home > database >  Why in Java documentation there are Instance functions described in an interface?
Why in Java documentation there are Instance functions described in an interface?

Time:06-06

As far as I know interfaces can not specify instance methods. Why are there methods clasified as 'instance methods' in Java documentation of interface Path?

https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/nio/file/Path.html (Method Summary)

CodePudding user response:

"Instance methods" are either "abstract methods" or "default methods". They're not a separate classification.

Compare the different tabs in the online documentation.

The user of the interface is presumably most interested in the viewpoint of "instance methods". The implementor of the interface further needs to distinguish abstract methods from default methods.

CodePudding user response:

Specifying instance methods is EXACTLY what interfaces do. You can expect that the documentation for an interface will describe precisely what each instance method should do. And the Javadoc that your question references does exactly that.

That way, the implementor of an interface (that is, the person who writes the class that implements it); and the user of an interface (that is, the person who writes code that calls the methods) can have a common understanding. They both agree on what the method is supposed to do, which is a different question entirely from how the method is actually implemented.

So if I'm going to write an implementation of the Path interface, I should follow the specification in the javadoc, and write a bunch of methods that do exactly what the interface promises. How they do it is my choice.

  • Related