Method Binding

For static typed programming languages,

          Class A:
            Def foo() { print "I'm in a" }
          
          Class B inherits A:
            Def foo() { print "I'm in b" }
            
          A test = new B() // test : A, contents are B's
          test.foo()       // which foo() runs?
          

Method Binding

For static typed programming languages,

          Class A:
            Def foo() { print "I'm in a" }
          
          Class B inherits A:
            Def foo() { print "I'm in b" }
            
          A test = new B() // test : A, contents are B's
          test.foo()       // which foo() runs?
          
  • Static Method Binding: it runs A's because x is A (known at compile time)
  • Dynamic Method Binding: it runs B's because the contents are B's (known at runtime)

Method Binding

For dynamic typed programming languages,

          Class A:
            Def foo() { print "I'm in a" }
            Def bar() { call foo() }
          
          Class B inherits A:
            Def foo() { print "I'm in b" }
            // Note: B inherits A's bar()
            
          test = new B() // test has no type or inferred type (i.e. B)
                         // but the contents are B's
          test.bar()     // which foo() runs?
          

Method Binding

For dynamic typed programming languages,

          Class A:
            Def foo() { print "I'm in a" }
            Def bar() { call foo() }
          
          Class B inherits A:
            Def foo() { print "I'm in b" }
            // Note: B inherits A's bar()
            
          test = new B() // test has no type or inferred type (i.e. B)
                         // but the contents are B's
          test.bar()     // which foo() runs?
          
  • Static: it runs A's because at compile time, bar() is calling A's foo() by definition.
  • Dynamic: it runs B's because at runtime, we know more and see B also has a foo() to call.