meeting -*- outline -*- * Motivations for OO Q: Why do people like OO? ** History *** Researchers in the 60's wanted to segment their code and data together. *** Simula 67 Needed to organize complex simulation code (for a nueclar power plant) With simualations, you're constantly changing the program to accomodate new data, ideas, or to try to find out other things This is a central theme in OOP: the need to accomdate changes, to allow for programs to evolve and change easily *** How AI research spinoffs benefitted OOP In AI, people do a lot of exploratory programming, i.e., they write a program and see what happens also needed to make changes easily and quickly *** change is important esp. in areas like user interfaces, simualation, graphics, AI, etc. ** Why OO is exciting? *** other approaches to programming don't deal with change well example, stepwise refinement (Wirth) ------------------------------------------ PRACTICAL LIMITATIONS OF TOP-DOWN DESIGN (STEPWISE REFINEMENT) Designed to fix decisions: requirements -> specs -> code -> test -> deliver change in requirements ==> redesign ------------------------------------------ spec = contract (signed) after contract is signed customer can't ask for more changes. *** why is change hard to deal with? Q: Why is change hard to deal with in programming? because of the complexity of programs (easy to change simple things), because we can't start over when we make changes *** OO deals with complexity through abstraction We organize software into abstraction layers, so that we can understand the whole system at an abstract level. We use procedural abstraction to hide algorithms, so changes in behavior are localized. We use information hiding (ADTs) to hide representations, so changes in data/information are localized. Such changes are *very* common. *** OO deals with extension of behavior and data through inheritance So we don't have to start over when we add new behavior (methods) or new data/information (fields). By inheritance, one can define a new class (ADT) by stating its differences from old classes. *** summary By abstraction, OO has the power to produce large complex software systems, by inheritance, OO has the ability to evolve and maintain such systems OOP is not about making programs run faster It's about how to organize code to support evolution and change ** The downside of OOP *** Slows down programs due to: - additional memory requirements to support - possibility of memory leaks or g.c. - method calls do lookup (indirection) in general to find code *** Learning curve lots of abstractions mean lots of things to learn ** Examples *** Modeling physical entities For example: Customers, Vehicles etc with their physical attributes give example in Java and C++ of a class Person and subclass Customer use a constructor and a method "greet" ------------------------------------------ JAVA CLASSES FOR PERSON AND CUSTOMER public class Person { private String name; public Person (String n) { name = n; } public String greet() { return "hello"; } /* ... */ } public class Customer extends Person { private String itemSought; public String greet() { return "Hi. I'm looking for " + itemSought; } } ------------------------------------------ *** Abstract concepts For example: Network connections, Database, points transactions with attributes like IP address, protocol etc. ** Object sematics (model of how it works) *** What happens when an object is created? ------------------------------------------ OBJECT CREATION Person p = new Person("Baby"); ------------------------------------------ ... show picture of memory allocation on the heap p [ *-]------> [ class | *-]----------> [ classname | *-]--> "Person" [ name | *-]-> "Baby" [ parent | *-]--> [ Object ] [ vtable | *-]---> (...) Do something similar for creation of a Customer object. Be sure the show the parent from the customer object's class pointing to the person class. Be sure to show the customer has both a name and an item sought! *** What happens when a method is called lookup in the object's class's vtable for code, followed by a procedure call ------------------------------------------ METHOD CALLS Person p; p.greet() if (randomCondition()) { p = new Person("Alice"); } else { p = new Person("Bob"); } p.greet(); ------------------------------------------ Talk about the method calls above, and show on the pictures of the objects and classes how it finds the appropriate method. Note that there is no way to tell before run-time the type for the last call ** Method inheritance (super) *** basics Suppose we had written Customer as follows ------------------------------------------ METHOD INHERITANCE public class Customer extends Person { private String itemSought; public String greet() { return super.greet() + " I'm looking for " + itemSought; } } ------------------------------------------ describe what happens. In C++ this would be: class Customer : public Person { private: string itemSought; public: string greet() { return Person::greet() + " I'm looking for " + itemSought; } } *** a more complex example (if you have time) If you have time, show a more complex example, where the methods call back and forth between super and subclass: ------------------------------------------ A MORE COMPLEX EXAMPLE public class C { public C() {} public String myClass() { this.myName(); } public String myName() { this.strC() } public String strC() { return "C"; } } public class D extends C { public D() {} public String strC() { return "D"; } } FOR YOU TO DO (STUDENTS) What is the result of: 1. new C().myClass()? 2. new D().strC()? 3. new D().myName()? 4. new D().myClass()? ------------------------------------------ draw pictures if that helps