0

I have started study about nio.2 in java 8 from java documentation. When I study about java.nio.file.Path, java documentation's first line is

The Path class, introduced in the Java SE 7 release.

which mean Path is a class, but when I look here I found that Path is an interface.

So why java documentation says that it is a class.

My another doubt is if Path is an interface then how Path methods (like getRoot() isAbsolute() and all other) work, because there is no implementation of methods of Path interface.

I know asking two different question in one statement is cumbersome but I have no idea how these two questions can be separated.

Edit: This question can't be duplicate of this, because in this question the questioner asked for implementation of Path interface, but here I'm asking how methods of this interface works, I mean is it internally executed by the JVM or any other mechanism is used to execute them.

Community
  • 1
  • 1
Sachin Kumar
  • 781
  • 1
  • 9
  • 28

2 Answers2

5

Path is an ordinary interface that is implemented like any other interface by a concrete class that declares to implement it and provides concrete methods for the abstract methods of the interface. So there’s nothing special with the methods of Path. As the linked question explains, there are ordinary implementations of this interface.

You shouldn’t get confused because it is called “class” in the documentation. While class in the narrowest sense is a type distinct from interface or enums, these types are all classes in the broadest meaning of the term. This is reflected by the fact that they all are stored within a class file and loaded via an operation name loadClass on a ClassLoader. At these places, no distinction between interfaces and classes is made. From this point of view, interfaces and enums are just classes with special properties (and similar, annotations are interfaces with special properties).

In documentations it makes sense to use the term “class” in the broader sense when the way you use it doesn’t differ, i.e. you are calling methods on a Path instance without having to care about whether the Path type is an interface. A difference has to be emphasized only when the reader is the one who has to implement it.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
  • But if i will call a method say getRoot() on path object, now how this method will executes, who executes it (i.e JVM or OS). – Sachin Kumar Feb 18 '15 at 09:58
  • That is a strange question. The *CPU* will execute the code. If you want to know whether the method is implemented using native code or Java code; that is unspecified as `Path` is an `interface`. Each concrete implementation may implement each method as it likes, `native` or using plain Java. `UnixPath` might do it differently than `WindowsPath` and even if they do it the same way, there is `ZipPath` provided by the `ZipFileSystem` which could do it differently. Any you may implement `Path` by yourself if you wish. – Holger Feb 18 '15 at 17:27
1

Path is an interface because the concrete implementation depends on the underlying file system. The methods are in classes which implement the interface, these are platform-dependent.

Note that you never contruct a Path object with new, but use a method like Paths.get, which returns an instance of the appropriate class.

For example, in Oracle's implementation, paths in windows are implemented by sun.nio.fs.WindowsPath (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/sun/nio/fs/WindowsPath.java)

Adrian Leonhard
  • 7,040
  • 2
  • 24
  • 38