Search code examples
javapathio

How does the isAbsolute method run[Java]?


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

Solution

  • 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.