Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is runtime binding?

Tags:

java

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

"An Intent is an object that provides runtime binding between separate components (such as two activities). "

Anyone care to explain what runtime binding is?

Thanks!

like image 538
PuffySparrow Avatar asked Dec 30 '13 00:12

PuffySparrow


People also ask

What is called as runtime binding?

1. 1. From what is intent in android?: "API calls are compile time binding while intent-based calls are run-time binding."

What is runtime binding in C++?

Early binding and Late binding in C++ The binding means the process of converting identifiers into addresses. For each variables and functions this binding is done. For functions it is matching the call with the right function definition by the compiler. The binding is done either at compile time or at runtime.


1 Answers

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

like image 131
Ashish Avatar answered Sep 16 '22 15:09

Ashish