CSC302 2006S, Class 16: Introduction to Object-Oriented Problem Solving Admin: * It's transition time! * Functional to object-oriented. * Deep and theoretical to practical (but not shallow) * Reading: /Beyond Java/, Chapters 1-3 * Homework coming soon. Overview: * What is object-oriented programming? * Three key issues: Encapsulation, Inheritance, Polymorphism. * Why OOP? * A biased history. * Polymorphism vs. Overloading. /What is OOP?/ A programming technique in which we group data and operations on those data into things called "objects". Programs consist of a group of objects that interact by sending messages to each other (i.e., calling methods). /Key Issues/ Encapsulation - Shove data and functions together. * "Protective coating" - Also protects that data from outside manipulation - Potentially protects client from having to know the implementation Inheritance - A class or object (automatically) gets methods and fields from another class. public class Base { ... } public class Subclass extends Base { ... } // Subclass automatically includes all the fields and methods of Base, without any additional work on the part of the programmer public class Alternate { Base tmp; ... } // Alternate // Alternate "includes" all the fields and methods of Base and can access them by calling tmp....; // Clients of Alternate may not have access to those methods, unless the implementer writes stuff like public foo() { tmp.foo(); } Polymorphism - You don't need to define the type of something when you use it. (More precisely, many things can be used for the same type.) * One example: If you write something that squares a value by multiplying it by itself, it only needs something that knows how to multiply. /Why OOP?/ * Seems more intuitive? * Easier to manipulate code after version 1 comes out? * Encapsulation: Can use the class in new contexts. * Inheritance: Can extend a class with new features. * Polymorphism: Can use the extended class in place of the original * TO summarize: Makes programs more extensible/reusable * Appropriate for big systems * Reuse * Potentially paralelizable * Easier to segment a large program into smaller parts * Many systems actually need to model real objects * Wal-Mart checkout line predictions * Objects provide a nice model for event-driven and GUI programming /History with Biases/ * What pair of languages does the following quote describe? The principle extensions which convert ___ to ___ provide the ability to: 1. Declare a class 2. Generate objects of the declared class 3. Name the generated objects 4. Form a hierarchical structure of class declarations * Stroustrup on C++? * Various folks on CLOS? * Sun folks about Java? * Apple about Object Pascal * The answer: Nygaard and Dahl in early 1960's, turning Algol int Simula Sutherland's Sketchpad (Interactive graphics program) - Platonic: You design a general image and can make many copies on the screen. - Cchanges to the general image affect the copies - New images can be defined in terms of old Simula and sketchpad both had minimal initial impact Mid 1970's: Alan Kay reflected on Simula, Sketchpad, Doug Englebart, and the future of computing * Computers will be more ubiquitous * Need to design computers for children * Computers should be portable and have an interface that includes pointing (Dynabook) * The language should tie all of this together (Smalltalk) The langauge, Smalltalk * Usable by children * Powerful enough for "real" computation, too. * Relatively pure; Very little (if any) imperative stuff * No distinguishing between language and enviornment/OS * (The GUI was the source of the ideas of MacOS and then Windows) OOP remained a mostly-academic area until late 1980's. Bjarne Stroustrup at Bell Labs said "Add objects to C++" * Amazingly successful * But ... * Doesn't get rid of harder parts of C (e.g., pointers) * made wrong decision on inheritance * awkward encapsulation mechanism Successor efforts: Java /Detour: Multiple vs. Single Inheritance/ * In some object-oriented languages (like Java), a class can only extend one other class. * In others (like C++), a class can extend multiple classes. Suppose we've designed a "pair of numbers class" and a "drawable object" class. * A point is a pair of numbers and drawable. public class Point extends PairOfNumbers extends Drawable { ... } * But, what happens if both PairOfNumbers and Drawable provide a method with the same name? Which does Point get? * Conclusion after analyzing lots of code: Rare that you need multiple inheritance, so throw it away and subsittute alternatives /Polymorphism vs. Overloading