Dynamic Binding
Here is a step-by-step guide on how to deal with dynamic binding.
Summary of Steps
Dynamic binding can be split into 2 parts -- compile time where the method descriptor is determined and runtime where the method is being invoked
Compile Time
During compile time, the method descriptor is recorded.
Steps
Determine compile-time type of target curr i.e. CTT(curr)
Determine compile-time type of arg obj i.e. CTT(obj)
Find all methods with same name foo that are accessible in class CTT(curr)
Includes superclasses
Appropriate access modifiers (public)
From above methods, find those compatible with CTT(obj)
Same number of parameters
Argument is supertype of CTT(obj)
From above, choose most specific method
If none is most specific, compilation error
Store method descriptor of most specific method
Runtime
During runtime, compiler tries to invoke the method according to method descriptor stored.
Steps
Retrieve method descriptor from compile-time step
Determine runtime type of target curr i.e. RTT(curr)
Starting from RTT(curr) class, find 1st method that match the method descriptor as retrieved from Step 1.
If not found, go up the heirachy of class to find
Runtime type
Remember that we do not consider the runtime type of the argument passed in. The method descriptor was already decided during compile time.
Step-by-step
Example 1
This is an example where a method is successfully invoked.
Example 1 boolean contains ( Object [] array , Object obj ) {
for ( Object curr : array ) {
if ( curr . equals ( obj )) {
return true ;
}
}
return false ;
}
Compile Time
Step 1 Step 2 Step 3 Step 4 Step 5
boolean contains ( Object [] array , Object obj ) {
for ( Object curr : array ) {
if ( curr . equals ( obj )) {
CTT(curr)
return type
method name
CTT(obj)
Object
boolean
equals
Determine compile-time type of target curr i.e. CTT(curr)
From line 2, CTT(curr) = Object
boolean contains ( Object [] array , Object obj ) {
for ( Object curr : array ) {
if ( curr . equals ( obj )) {
CTT(curr)
return type
method name
CTT(obj)
Object
boolean
equals
Object
Determine compile-time type of target curr i.e. CTT(curr)
From line 2, CTT(curr) = Object
Determine compile-time type of arg obj i.e. CTT(obj)
From line 1, CTT(obj) = Object
class Object {
..
public boolean equals ( Object obj ) {
// implementation omitted
}
}
Class
return type
method name
CTT(obj)
Object
boolean
equals
Object
Determine compile-time type of target curr i.e. CTT(curr)
From line 2, CTT(curr) = Object
Determine compile-time type of arg obj i.e. CTT(obj)
From line 1, CTT(obj) = Object
Find all methods with same name equals that are accessible in class Object
Only 1 found here, and is public
class Object {
..
public boolean equals ( Object obj ) {
// implementation omitted
}
}
Class
return type
method name
CTT(obj)
Object
boolean
equals
Object
Determine compile-time type of target curr i.e. CTT(curr)
From line 2, CTT(curr) = Object
Determine compile-time type of arg obj i.e. CTT(obj)
From line 1, CTT(obj) = Object
Find all methods with same name equals that are accessible in class Object
Only 1 found here, and is public
Is the method compatible with CTT(obj)?
Method takes in an Object, Object <: Object, compatible
class Object {
..
public boolean equals ( Object obj ) {
// implementation omitted
}
}
return type
method name
param type
boolean
equals
(Object)
Determine compile-time type of target curr i.e. CTT(curr)
From line 2, CTT(curr) = Object
Determine compile-time type of arg obj i.e. CTT(obj)
From line 1, CTT(obj) = Object
Find all methods with same name equals that are accessible in class Object
Only 1 found here, and is public
Is the method compatible with CTT(obj)?
Method takes in an Object, Object <: Object, compatible
Find most specific method
Since there is only 1, it is the most specific
Method descriptor stored is boolean equals(Object)
Runtime