CSC 123 Study Guide to the final exam. The final exam will be targeted to be about 1 to 1.5 hours in length. It will NOT be comprehensive but will include some material that were covered on the midterm (inheritance, visitor pattern, adapter pattern). Topics to Study: Inheritance. Static versus runtime types of objects and their impact. Static versus dynamic dispatch (virtual, void, new in C#) Type casting rules. How they're different in C#/Java and C/C++ The use of interfaces - how are interfaces different from superclasses Defining data structures using interfaces and inheritance (e.g. defining linked lists as consisting of a class for nil and classes for non-nil cells). The Visitor Pattern. Polymorphism. Natural v.s.artificial polymorphism Inheritance polymorphism versus parametric types (generics like ). (read the MSDN article on .Net generics). Types (this obviously also ties in with what's above) The use of types in languages (what are they for) Static versus dynamic type checking. What does it mean for a language to be "type safe" - that is, what are the consequences of untyped languages such as scheme/perl. I've decided, however, to NOT test you on the type inference calculus since we didn't spend enough time on it. However, you must read the "class notes on type inference" that I wrote and understand the ideas there. Aspect-Oriented Programming and AspectJ Know how to define pointcuts and advice. You don't have to know every pointcut, but at least call, execution, set, get, within/withincode/cflow, args, target, this, and composition using &&, || and !. The purpose of aspect oriented programming - what it's trying to give us that we don't already have. What to Study: Class notes Sample programs Past assignments The class notes are the most important. There are a lot of subtle points about inheritance that are not shown by the sample programs (because they work, and don't show what doesn't work). How to Study: Ask professor for help (I won't bite and hopefully you won't either). Do the sample problems here without looking at the answer. Do NOT memorize definitions or simply learn the mechanics without understanding their purpose. ---- Sample questions to prepare for the final exam ---- Answers to be posted later - try them first. 0. The last two problems on the midterm, reproduced here: Suppose that in C#, classes B and C are both subclasses of A, and that B and C are otherwise unrelated. Assume also that: A x = new B(); B y = new B(); Determine if the following lines are valid, and if not, whether a compile-time or runtime error will be generated (consider the lines independently). Explain your answer. a. C z = (C)x; b. C z = (C)y; --- In C#, static and dynamic dispatch are distinguished using certain keywords (virtual, override and new). Suppose you are learning a new language that has C#/Java style syntax, but which does not have these keywords. You look in your notes and see that you had copied some code that the professor had written on the board: class super { public void f() { print("super f"); } } class sub : super { public void f() { print("sub f"); } } Unfortunately, you dozed off afterwards and don't remember if the professor said the language used static or dynamic dispatch. You need to conduct an experiment to find the answer yourself. Write a code fragment that will behave differently depending on whether static or dynamic dispatch is used. {\bf Clearly indicate which is which.} You don't have to write a ``main'' function - just the lines that matter. 1. Given the following: interface I { I f(); } class A : I { public virtual I f() { return this; } } Explain what's wrong with the following lines: A x = new A(); A y = x.f(); 2. In the visitor pattern we had a critical function: public object accept(visitor v) { return v.visit(this); } Where was this function defined (in what class(es) does it exist?) What is "this" referring to? 3. Explain the difference between natural and artificial polymorphism. Give an example of each. 4. Explain in your own words one advantage of parametric polymorphism over inheritance polymorphism. That is, what's the difference between using a type parameter , and calling your variables (for example) "A x" instead of "Object x". 5. Some people are of the opinion that untyped languages such as Scheme and Perl are better for implementing polymorphism, and that types just get in the way. That is, in Scheme for example, one can define the length function as: (define (length l) (if (null? l) 0 (+ 1 length(cdr l)))) There's no mention of types and obviously the function will work with any kind of list. Types, even parametric types, simply gets in the way of programming. Regardless of whether you agree with this opinion, there are issues that this view is not taking into consideration. What are they? Sample AspectJ problems: 6. Write a pointcut definition to pick out attempts to access any public variable of a class A. By pointcut definition I mean something in the form pointcut name(...) : ... 7. Assume that classes C and D both have a function void f(int). Write a pointcut expression that picks out calls to either function. Also capture the argument passed with the pointcut. 8. Given class: class B { private int x; pubilc B(int x0) { x=x0; } // constructor public int f(int n) { if (n<2) return 1; else return n*f(n-1); } public void g(int y) { System.out.println(x+f(y)); } } a. Write a pointcut that picks out the "execution" of the constructor of B. (when the constructor is called, the object doesn't exist yet). b. Write a pointcut that picks out the initial call to f (as opposed to recursive calls). c. The following pointcut and advice tries to change the parameter passed to g. Explain what's wrong with it the way it's written. DON'T JUST CORRECT IT; *EXPLAIN* WHY IT'S WRONG THE WAY IT IS! before(int y) : call(void B.g(int)) && args(y) { g(y+1); } (hint: there are three problems that need to be addressed). d. Write an advice that throws an error if the g function is called from anywhere except main (public static void *.main(..)) 9. Explain the difference between "withincode" and "cflow" pointcuts.