CORRECTION to virtual function resolution answer FOR HW4: 5/6/16 BGR The hierarchy is clearly stated in the problem and the typo resulted in d=new E() replacing D d; d=new F() which is illegal in Java because F objects do not have an is-a relation to D objects since D is not an ancestor class of F in the hierarchy. Now in looking over the posted answer I have found that most of the RTA answers are in error because I did not consider all the effects of changing new F() to new E(). sorry Let's reason through the correct answers for RTA on #1, #2, and for both CHA and RTA on #3 and #4 in III. #1. a1.foo(). The CHA answer is not affected by changing the objects created in the code -- this virtual call might be resolved to any of the functions foo() defined in the cone(A) because a1 might point to an object of any of these classes. So the answer remains: A.foo(), B.foo(), E.foo(). For RTA, we know that A,G, and E objects have been created in the code. Therefore, we need to determine what the resolved method for a1.foo() will be if a1 points to an A, G, or E object. if a1 points to an A object then A.foo() will be called. if a1 points to a G object, then again A.foo() will be inherited by class G and be called. if a1 points to an E object then E.foo() will be called. So RTA will resolve this call to A.foo() and E.foo(). #2. a2.bar(). The CHA answer is not affected by changing the objetcs created in the code (as in case #1). So the answer remains A.bar(), B.bar(), F.bar(), G.bar(). However, for RTA we know that A,G, and E objects have been created in the code. Therefore a2.bar() would resolve to A.bar() when a2 points to an A object. a2.bar() would resolve to G.bar() when a2 points to a G object, and a2.bar() would resolve to B.bar() when a2 points to an E object. So RTA reports A.bar(), G.bar(), B.bar() as possible targets of this virtual call, only eliminating F.bar() from CHA's list. #3. to resolve d.foo() CHA first looks at class D for a method foo(). Finding none, it proceeds up the class hierarchy looking for the nearest ancestor class for D, including a foo() function and finds B.foo(). B.foo() is the correct resolved function if d points to a D object (as you suspected). CHA also considers the possibility that the d reference variable may actually point to an object of a subclass of D during execution -- that is, d may point to an E object. if d points to an E object, then the virtual call will resolve to E.foo(). So the CHA answer will be that d.foo() may resolve to either B.foo() or E.foo() at execution time. To resolve d.foo() by RTA, we need to do what CHA already does, but also we consider whether objects in classes D or E have been instantiated. Here only an E object has been created in the code (somewhere), so RTA only reports E.foo() as the possible resolved method call. (Note: if both an E and a D object had been created in the code, then RTA would report the same resolved method set as CHA. #4. to resolve b.bar() in CHA, we have to look at all possible bar() functions in the cone(B) (Note; we don't need to look at B's ancestors because B itself contains a bar() method.) So, CHA finds that b.bar() might be resolved to B.bar() or F.bar(). Now since the correction was done, it is clear that no F objects are created which means that F.bar() will not be reported by RTA. However, an E object is created, and if d pointed to that E object, then B.bar() would be the inherited bar() function that would be called. So the correct answer here for RTA is B.bar() (NOT E.bar as in the handwritten answer -- since E.bar() does not exist! sorry)