Docsity

Dynamic Dispatch and Polymorphism in Java: Apparent and Actual Types, Study notes of Computer Science

Toggle sidebar
bg1
Dynamic Dispatch
CS 340, Reed, Fall 2004
Examples and notes taken in large part from:
[http://6170.lcs.mit.edu/www-archive/Old-Fall03/lectures/handout/lec03_subclassing.pdf]
[http://www.cs.sunysb.edu/~cse219/lectures/5 ]
Review: Polymorphism Example
Note that given an Account and AccountPlus class that extends Account, we can create an array of Account
objects, where at run time it is determined whether each one is an Account or an AccountPlus instance
public static void main(String[] args) {
Account [ ] accounts = new Account[3];
accounts[0] = new Account("checking");
accounts[1] = new AccountPlus("checkguard", 200);
accounts[2] = new Account("savings");
// charge the monthly fee for each account
for (int i = 0; i < accounts.length; i++) {
Trans fee = new Trans(-1, new Date());
if (accounts[i].checkTrans(fee))
accounts[i].post(fee);
}// end for
}
Apparent vs. Actual Type
In the example above, each of the three accounts instances have an apparent type of Account, however
accounts[1] has an actual type of AccountPlus.
Only the apparent type’s methods are available.
Remember that Java guarantees the apparent type of any object is a supertype of its actual type
Examples: what are the apparent type and actual type of each of the following?
Vector v = new Vector(); // what are v’s apparent & actual types?
Object o1 = v; // what are o1’s apparent & actual types?
int[] a = {1, 2};
Object o2 = a; // what are o2’s apparent & actual types?
pf2

Partial preview of the text

Download Dynamic Dispatch and Polymorphism in Java: Apparent and Actual Types and more Study notes Computer Science in PDF only on Docsity!

Dynamic Dispatch

CS 340, Reed, Fall 2004

Examples and notes taken in large part from:

[http://6170.lcs.mit.edu/www-archive/Old-Fall03/lectures/handout/lec03_subclassing.pdf] [http://www.cs.sunysb.edu/~cse219/lectures/5 ]

Review: Polymorphism Example

Note that given an Account and AccountPlus class that extends Account, we can create an array of Account objects, where at run time it is determined whether each one is an Account or an AccountPlus instance public static void main(String[] args) { Account [ ] accounts = new Account[3]; accounts[0] = new Account("checking"); accounts[1] = new AccountPlus("checkguard", 200); accounts[2] = new Account("savings"); // charge the monthly fee for each account for (int i = 0; i < accounts.length; i++) { Trans fee = new Trans(-1, new Date()); if (accounts[i].checkTrans(fee)) accounts[i].post(fee); }// end for }

Apparent vs. Actual Type

In the example above, each of the three accounts instances have an apparent type of Account , however accounts[1] has an actual type of AccountPlus.  Only the apparent type’s methods are available.  Remember that Java guarantees the apparent type of any object is a supertype of its actual type  Examples: what are the apparent type and actual type of each of the following? Vector v = new Vector(); // what are v’s apparent & actual types? Object o1 = v; // what are o1’s apparent & actual types? int[] a = {1, 2}; Object o2 = a; // what are o2’s apparent & actual types?

Dispatching Dispatching in Functional Languages (e.g. C)  At compile time, the destination method is known.  At compile time, a method call is translated into a jump/branch command to a pre-calculated address.  We can't do that in Java, since the type of the calling object may not be know until run-time. Dispatching in Java

  • Any method to be executed is based on actual type of object, not its apparent type
  • Actual types are known only at run-time, so methods are determined only at runtime.
  • How does the compiler know how to translate a method call at compile time if it only knows the apparent type?
    • Each object contains a reference to a dispatch vector.
    • The dispatch vector contains an entry for each of the object’s methods.
    • Each of these entries is a reference to the specific code for that method.
  • When the compiler must generate code for a method call, it does the following: [http://www.cs.mcgill.ca/~cs303/winter2004/notes/lecture9.pdf]

1. Extract the method name and signature from the method call.

2. Search the dispatch vector of the stack object to find our target memory address.

3. Use the method signature to remove arguments from the stack and transfer them to

the method's local variables.

4. Transfer control to target memory address.

  • The reference to the dispatch vector and the references to methods in the dispatch vector are all generated at run time based on the actual type of the object. Consider the example for different bank accounts: Account [ ] accounts = new Account[3]; accounts[0] = new Account("checking"); accounts[1] = new AccountPlus("checkguard", 200); Accounts[0]:Account String name Vector transv balance:int = 0 Dispatch Vector Accounts[1]:AccountPlus String name Vector transv Balance:int = 0 c reditLimit:int = 200 Dispatch Vector checkTrans(trans t) post (Trans t) checkTrans(trans t) post (Trans t) AccountPlus. checkTrans Account. checkTrans Account.post “checking” (^) “checkguard”