0

The isAbsolute method does not have a body in the path interface, but I can run it in the following code. How is this possible?

Path path= Paths.get("D:\\Example\\1.txt");
    System.out.println(path.isAbsolute());//prints true
mramz
  • 11
  • 2
  • 3
    Because `Paths.get` returns an object that implements the `Path`-interface. – marstran Jul 12 '21 at 13:47
  • Are you interested in the implementation of the mechanism, or the concepts? For the implementation, see: https://stackoverflow.com/questions/1543191/method-overriding-in-java – Hulk Jul 12 '21 at 13:57
  • For the concepts, see https://stackoverflow.com/questions/12374399/what-is-the-difference-between-method-overloading-and-overriding and the questions linked from there. – Hulk Jul 12 '21 at 13:59

1 Answers1

0

Imagine following code:

public interface Foo {
  public boolean bar();
}

public class Fooz implements Foo {
  @Override
  public boolean bar() {
    return false;
  }
}

And:

public Foo getFoo() {
  return new Fooz();
}

public static void main(String[] args) {
  Foo myFoo = getFoo();
  System.out.println(myFoo.bar()) //false
}

If you are looking for actual implementation of this function, I advise looking thru the source code of JVM of your choosing. Example of one such implementation can be found in UnixPath.java on OpenJDK github repo.

Kryštof Vosyka
  • 566
  • 3
  • 15
  • I understand what you mean, but the problem is that if you follow this method, the method will not work. Paths.get () Returns the Path.of () method. The of() method is also returns FileSystems.getDefault ( ) .getPath (first, more). getDefault () itself Returns a FileSystem object that calls the getPath () method. This method returns a Path object, but it is not yet clear how the isAbsolute method is implemented. – mramz Jul 12 '21 at 14:16
  • What do you mean *the method will not work*? Your question seems to indicate that you wanted to know how it was even possible to call `isAbsolute`, but are you actually looking for the source code of the implementation of `isAbsolute`? – Henry Twist Jul 12 '21 at 14:43
  • Yes, that's what I mean. I want to know how this is implemented.[looking for source code] – mramz Jul 12 '21 at 15:22