Java – what is runtime binding?
I was browsing the Android development training document and came across:
"Intent is an object that provides runtime binding between different components (for example, two activities)."
Anyone cares about explaining what runtime bindings are?
thank you!
Solution
Inheritance creates type compatibility
A superclass reference refers to an object of a subclass, which can only be used to access methods that inherit and override subclasses A newly defined member in a subclass cannot reference a superclass
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 this sentence
ref.f2();
Here ref is the reference of class A, which has the address of class B object. F2 () is an rewriting method
When the compiler detects such a statement, it then does not bind the function call to any definition It only verifies calls
The binding of these calls is left to the runtime environment When the program runs, the system recognizes the data type of the object and binds the object class provided by the function call and function definition The binding between this type of function call and function definition is called "late binding" or "runtime binding" or "runtime polymorphism" or "dynamic method scheduling"