<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * This program demonstrates that instance method names are resolved at runtime
 * using the runtime (dynamic) type of the object, whereas instance field names
 * are resolved at compile time using the compile-time (static) type of the
 * expression.
 * 
 * Inside the method f, the compile-time type of x is Foo, and this is the
 * information used to access x.a; whereas when f is called with an object of
 * type Bar, this is the information used to determine which b() method to
 * dispatch.
 */

public class ShadowTest {
   public static void main(String[] args) {
      f(new Bar());
   }

   static void f(Foo x) {
      System.out.println(x.a);
      System.out.println(x.b());
   }
}

class Foo {
   String a = "Foo";

   String b() {
      return "Foo";
   }
}

class Bar extends Foo {
   String a = "Bar";

   String b() {
      return "Bar";
   }
}
</pre></body></html>