CSC153 2004S, Class 28: Introduction to Object-Oriented Programming Admin: * Books in or not in the bookstore. * We start Java on Wednesday. * Questions on the exam? ; What's wrong with the following? (define list-clones? (lambda (lst1 lst2) (and (equal? lst1 lst2) (not (eq? lst1 lst2))))) Answer: It doesn't check recursively, so lst1 and lst2 can share some of their structure. Overview: * Review: What are the key aspects of functional programming? * About object-oriented programming If you were going to tell someone at Microsoft about the wonder of functional programming (Scheme), what would you say? * Anything you can do in one, you can do in the other. (Turing equivalence.) * Anything you can do easily in one, you may be able to do easily in the other, but you may not. * Impossible to program without recursion. * But makes recursion somewhat easier than most languages. * Modifying variable is "discouraged". Possible with * set! * set-car! * set-cdr! * vector-set! * More parentheses * Simpler syntax (at least fewer keywords) * Functions as first-class data type * Most languages don't let you build and return new functions "on the fly" * Procedure parameters are awkward in C (and C++) * Anonymous procedures are also awkward in C. * Symbols as first-class data type * Enumerated types are fairly similar * What are the advantages of symbols over strings etc? * Much faster comparison! (Memory location vs. char by char) * public static final int CAT = 1; * public static final int DOG = 2; if (CAT == thingy) { System.out.println("Meow"); } * Less sense of "do this, then do that, then do another thing" On to object-oriented programming (OOP) * Key idea in OOP: Programs are collections of communicating objects * Historical: * Simula (late 1960's): Pascal plus objects [Nygaard and Dahl, Norway] * Smalltalk (late 1970's): "Pure" object-oriented programming (everything, including control structures, is an object); Language as operating system * C++ (1980's): C incremented with object-oriented-like stuff. (Stroustrup) * Plus a "funny" joke: "Increment C, but get its old value" * x = ++y; * x = y++; * Java (1990's): C redone for large projects and embedded computing * "Sandboxing" * Better (enforced) type checking * Better error reporting through exceptions * Clever marketing * Many others, some of which rock, and some of which are Turbo Pascal. * What is an object? * Example: The pen in Erik's right hand is an object. * Things that have characteristics and do stuff * Local data plus associated functions * Reproducible * Three key ideas in object-oriented programming * Encapsulation - Programmer knows what objects do, but doesn't have to know how. (Programmer may not have access to "how"). Data/information hiding. * Inheritance - You can build new objects (or classes) from old classes. The new classes inherit the fields and methods of the original classes. * Code reuse. * Polymorphism - "Many forms": One procedure can be invoked on many different, but related, kinds of data. * Vs. overloading: You can define different procedures with the same name. * Code reuse