0

I am wondering why there isn't a dynamic binding in Java in method arguments? Example:

static class C {
   public static void test(C c) {
      System.out.println("C");
    }
}

static class D extends C {
   public static void test(D d) {
      System.out.println("D");
    }       
}

public static void main(String[] args) {
        C c = new D();
        D d = new D();
        test(c);

It has to be anyway determined, whether variable c contains the instance of class C or the instance of its subclass, so why can't it be done dynamically?

coolchock
  • 99
  • 4
  • 1
    `static` opposite of dynamic? declaring a method `static` is declaring it as a class method, not *bound* to any instance - should `D.test()` call the method of `C` or `C.test()` the one of `D`? (despite your code is calling neither `test`) – user85421 Dec 11 '19 at 23:34
  • 1
    My best guess is that Java designers haven't got around to implementing it. This is something that is definitely possible to do, though: C#, which started in 2002 at the same place as Java, added `dynamic` keyword some time in 2010. – Sergey Kalinichenko Dec 11 '19 at 23:35
  • Of course you can always add two overloads of `test`, like [this](https://ideone.com/bmTfnk), but I think this goes against the grain of the language. – Sergey Kalinichenko Dec 11 '19 at 23:41

0 Answers0