Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COS 260 DAY 22 Tony Gauvin 1 Agenda • Questions? • 9th Mini Quiz corrected – Good results • Assignment 5 Not corrected yet • Assignment 6 Posted (one more) – Due Dec 3 • Finish Discussion on Further Abstraction techniques 2 Final Countdown • Today – Finish Chapter 10 • Nov 30 – 10th Mini quiz – Begin Chap 11 – Capstone Progress Report • Dec 3 – Finish Chapter 11 – Assignment 6 due Copyright © 2014 Pearson Education, Inc. • Dec 7 – 11th mini quiz – Begin Chapter 12 • Dec 10 – Finish Chapter 12 – Assignment 7 Due • Dec 14 @ 10 AM – 12th mini quiz – Capstone Presentations Slide 1-3 3 Further abstraction techniques Abstract classes and interfaces 5.0 Main concepts to be covered • Abstract classes • Interfaces • Multiple inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 5 Configuration of foxes • Similar simplifications to rabbits. • Hunting and eating could be modeled in many different ways. – Should food level be additive? – Is a hungry fox more or less likely to hunt? • Are simplifications ever acceptable? • Ex 10.13 to 10.21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 6 The Simulator class • Three key components: – Setup in the constructor. – The populate method. • Each animal is given a random starting age. – The simulateOneStep method. • Iterates over separate populations of foxes and rabbits. • Two Lists for each animal are used : – rabbits and newRabbits – foxes and newFoxes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 7 The update step for(Iterator<Rabbit> it = rabbits.iterator(); it.hasNext(); ) { Rabbit rabbit = it.next(); rabbit.run(newRabbits); if(! rabbit.isAlive()) { it.remove(); } } … for(Iterator<Fox> it = foxes.iterator(); it.hasNext(); ) { Fox fox = it.next(); fox.hunt(newFoxes); if(! fox.isAlive()) { it.remove(); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 8 Room for improvement • Fox and Rabbit have strong similarities but do not have a common superclass. • The update step involves similarlooking code. • The Simulator is (too) tightly coupled to specific classes. – It ‘knows’ a lot about the behavior of foxes and rabbits. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 9 Fixing The simulator Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 10 The <abstract> Animal superclass • Place common fields in Animal: – age, alive, location • Method renaming to support information hiding: – run and hunt become act. • Simulator can now be significantly decoupled. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 11 Revised (decoupled) iteration for(Iterator<Animal> it = animals.iterator(); it.hasNext(); ) { Animal animal = iter.next(); animal.act(newAnimals); // Remove dead animals from simulation if(! animal.isAlive()) { it.remove(); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 12 The act method of Animal • Static type checking requires an act method in Animal. • There is no obvious shared implementation. • Define act as abstract: abstract public void act(List<Animal> newAnimals); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 13 Abstract classes and methods • Abstract methods have abstract in the signature. • Abstract methods have no body. • MUST be overridden in Subclass • Abstract methods make the class abstract. • Abstract classes cannot be instantiated. • Concrete subclasses complete the implementation. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 14 The Animal class public abstract class Animal { fields omitted /** * Make this animal act - that is: make it do * whatever it wants/needs to do. */ abstract public void act(List<Animal> newAnimals); other methods omitted } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 15 Abstract Hierarchy Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 16 Problem 1 Ex 10.43-10.49 Bonus 10.50 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 17 Further abstraction Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 18 Multiple inheritance • Having a class inherit directly from multiple ancestors. • Each language has its own rules. – How to resolve competing definitions? • Java forbids it for classes. • Java permits it for interfaces. – No competing implementation. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 19 Selective drawing (multiple inheritance) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 20 An Actor interface public interface Actor { /** * Perform the actor's regular behavior. * @param newActors A list for storing newly created * actors. */ void act(List<Actor> newActors); /** * Is the actor still active? * @return true if still active, false if not. */ boolean isActive(); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 21 Classes implement an interface public class Fox extends Animal implements Drawable { ... } public class Hunter implements Actor, Drawable { ... } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 22 Interfaces as specifications • Strong separation of functionality from implementation. – Though parameter and return types are mandated. • Clients interact independently of the implementation. – But clients can choose from alternative implementations. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 23 Interfaces as types • Implementing classes do not inherit code, but ... • ... implementing classes are subtypes of the interface type. • So, polymorphism is available with interfaces as well as classes. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 24 Features of interfaces • • • • All methods are abstract. There are no constructors. All methods are public. All fields are public, static and final. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 25 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 26 Alternative implementations http://docs.oracle.com/javase/7/docs/api/java/util/List.ht ml Problem 2 Ex 10.58 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 27 The Class class • A Class object is returned by getClass() in Object. • The .class suffix provides a Class object: Fox.class • Used in SimulatorView: Map<Class, Color> colors; • String getName() for the class name. • http://docs.oracle.com/javase/7/docs/ap i/java/lang/Class.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 28 Review • Inheritance can provide shared implementation. – Concrete and abstract classes. • Inheritance provides shared type information. – Classes and interfaces. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 29 Review • Abstract methods allow static type checking without requiring implementation. • Abstract classes function as incomplete superclasses. – No instances. • Abstract classes support polymorphism. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 30 Review • Interfaces provide specification without implementation. – Interfaces are fully abstract. • Interfaces support polymorphism. • Java interfaces support multiple inheritance. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 31