I. Motivations for OO Why do people like OO? A. History 1. Researchers in the 60's wanted to segment their code and data together. 2. Simula 67 3. How AI research spinoffs benefitted OOP 4. change is important B. Why OO is exciting? 1. other approaches to programming don't deal with change well ------------------------------------------ PRACTICAL LIMITATIONS OF TOP-DOWN DESIGN (STEPWISE REFINEMENT) Designed to fix decisions: requirements -> specs -> code -> test -> deliver change in requirements ==> redesign ------------------------------------------ 2. why is change hard to deal with? Why is change hard to deal with in programming? 3. OO deals with complexity through abstraction 4. OO deals with extension of behavior and data through inheritance 5. summary C. The downside of OOP 1. Slows down programs 2. Learning curve D. Examples 1. Modeling physical entities ------------------------------------------ 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; } } ------------------------------------------ 2. Abstract concepts E. Object sematics (model of how it works) 1. What happens when an object is created? ------------------------------------------ OBJECT CREATION Person p = new Person("Baby"); ------------------------------------------ 2. What happens when a method is called ------------------------------------------ METHOD CALLS Person p; p.greet() if (randomCondition()) { p = new Person("Alice"); } else { p = new Person("Bob"); } p.greet(); ------------------------------------------ F. Method inheritance (super) 1. basics ------------------------------------------ METHOD INHERITANCE public class Customer extends Person { private String itemSought; public String greet() { return super.greet() + " I'm looking for " + itemSought; } } ------------------------------------------ 2. a more complex example (if you have time) ------------------------------------------ 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()? ------------------------------------------