What is runtime binding?

前端 未结 4 977
广开言路
广开言路 2021-02-09 03:58

I\'m going through the android development training docs and stumbled upon this:

\"An Intent is an object that provides runtime binding between separate

相关标签:
4条回答
  • 2021-02-09 04:00

    Runtime binding is the same thing as dynamic binding or late binding. Dynamic binding basically means that the method implementation that is actually called is determined at run-time, and not at compile-time. And that’s why it’s called dynamic binding – because the method that will be run is chosen at run time.

    0 讨论(0)
  • 2021-02-09 04:12

    Some short research could lead you to this http://en.wikipedia.org/wiki/Late_binding .

    Short definition to prevent some dead-linking: Runtime binding is a computer programming mechanism in which the method being called upon an object is looked up by name at runtime.

    0 讨论(0)
  • 2021-02-09 04:15

    Dynamic Binding refers to the case where compiler is not able to resolve the call and the binding is done at runtime only. Let's try to understand this. Suppose we have a class named 'SuperClass' and another class named 'SubClass' extends it. Now a 'SuperClass' reference can be assigned to an object of the type 'SubClass' as well. If we have a method (say 'someMethod()') in the 'SuperClass' which we override in the 'SubClass' then a call of that method on a 'SuperClass' reference can only be resolved at runtime as the compiler can't be sure of what type of object this reference would be pointing to at runtime.

    ...
    SuperClass superClass1 = new SuperClass();
    SuperClass superClass2 = new SubClass();
    ...
    
    superClass1.someMethod(); // SuperClass version is called
    superClass2.someMethod(); // SubClass version is called
    ....
    

    Here, we see that even though both the object references superClass1 and superClass2 are of type 'SuperClass' only, but at run time they refer to the objects of types 'SuperClass' and 'SubClass' respectively.

    Hence, at compile time the compiler can't be sure if the call to the method 'someMethod()' on these references actually refer to which version of the method - the super class version or the sub class version.

    Thus, we see that dynamic binding in Java simply binds the method calls (inherited methods only as they can be overriden in a sub class and hence compiler may not be sure of which version of the method to call) based on the actual object type and not on the declared type of the object reference.

    Copied from the following site

    0 讨论(0)
  • 2021-02-09 04:16

    Inheritance creates type compatibility. It allows a super class reference to refer to the object of sub class. (Reverse is not true).

    A super class reference, that refers to object of sub class, can only be used to access the inherited and overridden methods of sub class. The members newly defined in sub class are not accessible using reference of super class.

    class A  {
     void f1() {  //this holds address of object of B     
       System.out.println("A f1");
     }
     void f2() {
       System.out.println("A f2");
     }
    }//A
    
    
    class B extends A {
    
     void f3() {   //new method     
       System.out.println("B f3");
     }
    
     void f2() { //this holds address of object of B     
       System.out.println("B f2 starts");
       f3(); //this.f3()
       System.out.println("B f2 ends ");
    
     } }  //B
    
    
    class TypeCmptbl {
       public static void main(String args[]) {
          A ref; //reference of A
          ref = new B();//Object of B
    
          //ref.inherited()  allowed
          ref.f1();
    
          //ref.overridden() allowed
          ref.f2();
    
         //ref.newMembersOfChild() not allowed
        //ref.f3();
    
      }//main
    }
    

    Consider the statement

      ref.f2();
    

    Here ref is a reference of class A and it has address of object of class B f2() is a overridden method.

    When compiler detects such a statement then it doesn't bind the function call with any definition. It only validates the call.

    Binding of such calls is left for the runtime environment. At program runtime system identifies the datatype of the object and binds the function call with the function definition provided by the class of object. This type of binding between the function call and function defination is called as "late binding" or "runtime binding" or "runtime polymorphism" or "dynamic method dispatch".

    0 讨论(0)
提交回复
热议问题