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). Topics to Study: Inheritance. Static versus runtime types of objects and their impact. Type casting rules. How they're different in C#/Java and C/C++ Defining data structures using interfaces and inheritance (study the expression tree programs). The Visitor Pattern. The Adapter pattern. Polymorphism. The difference between natural and artificially polymorphic procedures. Generics (parametric types) in C#. How to use it. How it differs with C++ templates. What are its advantages and disadvantages compared to purely inheritance-based polymorphism (static type checking and avoidance of type casts). Be sure to read the MSDN article on .Net generics. Types (this also ties in with what's above) Know the two basic rules for type checking: f:A->B, t:A ----------- f(t):B [x:A] (assumed temporarily) . . t:B ------------------ lambda x.t : A->B Know what is the "Curry-Howard isomorphism." 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 !. Know how to call proceed inside around advice. Know how to use "inter-type" declarations". What is the meaning of "private" within an aspect? 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. 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, what kind of errors will be caused and how to correct it. 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: visitor or visitee?) What does "this" refer to? 3. Describe a situation in which an object adapter (aka adapter pattern) can be useful. Can you think of a situation outside of the food program? 4. Explain the difference between natural and artificial polymorphism. Describe an example of each type of procedure. When answering questions that require you to compose a paragraph, you cannot just memorize what I say verbatim. You must be able to put the answer in your own words. Not all questions I ask will be so direct. For example, problem 6 below is really asking you about the same thing as problem 4. Unless you truely appreciate the problem here you will not see that, and you will not know how to answer the question. There will be at least one question like problem 6, in which I ask you about something from a different angle. 5. 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". 5b. Assume that classes sub1 and sub2 both extend class super1. Consider the following alternatives for defining a polymorphic linked list class that can contain elements of either sub1 or sub2: class one { super1 item; // head, car one next; // tail, cdr } class two where A : super1 { A item; two next; } Write code to construct an instance of class two where item is of type sub1. Assume you wish to have a linked list M of sub1 objects and another list N of sub2 objects. Which class would you use and why? 6. 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: 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. 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(..)) f. Suppose an aspect contains the following intertype declarations: Aspect importantaspect { public int B.y; private int B.z; ... Explain the difference between public and private above (hint: they're not the same as public/private within an ordinary class). That is, what are the consequences with respect to other parts of the program. 9. Explain the difference between "withincode" and "cflow" pointcuts. 10. Explain the difference between the "this" and "target" pointcuts. The best way to answer this question is by using a specific example.