Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)

Similar presentations


Presentation on theme: "1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)"— Presentation transcript:

1 1 COS 260 DAY 22 Tony Gauvin

2 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more) –Due Dec 3 Finish Discussion on Further Abstraction techniques

3 3 Final Countdown Today –Finish Chapter 10 Nov 30 –10 th Mini quiz –Begin Chap 11 –Capstone Progress Report Dec 3 –Finish Chapter 11 –Assignment 6 due Dec 7 –11 th mini quiz –Begin Chapter 12 Dec 10 –Finish Chapter 12 –Assignment 7 Due Dec 14 @ 10 AM –12 th mini quiz –Capstone Presentations Copyright © 2014 Pearson Education, Inc. Slide 1-3

4 Further abstraction techniques Abstract classes and interfaces 5.0

5 5 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

6 6 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

7 7 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

8 8 The update step Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for(Iterator it = rabbits.iterator(); it.hasNext(); ) { Rabbit rabbit = it.next(); rabbit.run(newRabbits); if(! rabbit.isAlive()) { it.remove(); } … for(Iterator it = foxes.iterator(); it.hasNext(); ) { Fox fox = it.next(); fox.hunt(newFoxes); if(! fox.isAlive()) { it.remove(); }

9 9 Room for improvement Fox and Rabbit have strong similarities but do not have a common superclass. The update step involves similar- looking 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

10 10 Fixing The simulator Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 11 The 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

12 12 Revised (decoupled) iteration Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for(Iterator it = animals.iterator(); it.hasNext(); ) { Animal animal = iter.next(); animal.act(newAnimals); // Remove dead animals from simulation if(! animal.isAlive()) { it.remove(); }

13 13 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 newAnimals); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 14 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

15 15 The Animal class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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 newAnimals); other methods omitted }

16 16 Abstract Hierarchy Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Problem 1 Ex 10.43-10.49  Bonus 10.50

18 18 Further abstraction Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

19 19 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

20 20 Selective drawing (multiple inheritance) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 21 An Actor interface Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public interface Actor { /** * Perform the actor's regular behavior. * @param newActors A list for storing newly created * actors. */ void act(List newActors); /** * Is the actor still active? * @return true if still active, false if not. */ boolean isActive(); }

22 22 Classes implement an interface Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Fox extends Animal implements Drawable {... } public class Hunter implements Actor, Drawable {... }

23 23 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

24 24 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

25 25 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

26 26 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

27 27 Alternative implementations Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling http://docs.oracle.com/javase/7/docs/api/java/util/List.ht ml Problem 2 Ex 10.58

28 28 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 colors; String getName() for the class name. http://docs.oracle.com/javase/7/docs/ap i/java/lang/Class.html 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

29 29 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

30 30 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

31 31 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


Download ppt "1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)"

Similar presentations


Ads by Google