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)

Slides:



Advertisements
Similar presentations
Comp1004: Object Oriented Design II Designing Applications Based on BlueJ Chapter 13.
Advertisements

Fields, Constructors, Methods
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Further abstraction techniques Abstract classes and interfaces 5.0.
Improving structure with inheritance
Further abstraction techniques Abstract classes and interfaces 1.0.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions Looking inside classes 3.0.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More about inheritance Exploring polymorphism 5.0.
Further abstraction techniques Abstract classes and interfaces 3.0.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
More about inheritance Exploring polymorphism 3.0.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)
OOP (Java): Abstract/ OOP Objectives – –use a foxes-and-rabbits simulation to introduce abstract classes, interfaces, and multiple.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Object Oriented Design: Identifying Objects
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
1 COS 260 DAY 1 Tony Gauvin. 2 Agenda Class roll call Instructor Introduction Instructor’s Educational Philosophy Contract on Classroom Behavior Syllabus.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Further abstraction techniques
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
1 COS 260 DAY 24 Tony Gauvin. 2 Agenda Questions? 10 th Mini Quiz Corrected –So-so results Capstone progress report over due Course evaluations Assignment.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
1 COS 260 DAY 21 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Further abstraction techniques Abstract classes and interfaces 6.0.
11 Further abstraction techniques
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 13 Tony Gauvin.
Understanding class definitions
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
Advanced Java Programming
COS 260 DAY 19 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
Chapter 14 Abstract Classes and Interfaces
COS 260 DAY 23 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 6 Tony Gauvin.
Further abstraction techniques
Improving structure with inheritance
Presentation transcript:

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) –Due Dec 3 Finish Discussion on Further Abstraction techniques

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 10 AM –12 th mini quiz –Capstone Presentations Copyright © 2014 Pearson Education, Inc. Slide 1-3

Further abstraction techniques Abstract classes and interfaces 5.0

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

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 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 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 Fixing The simulator Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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

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

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 Selective drawing (multiple inheritance) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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. newActors A list for storing newly created * actors. */ void act(List newActors); /** * Is the actor still active? true if still active, false if not. */ boolean isActive(); }

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

27 Alternative implementations Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling ml Problem 2 Ex 10.58

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. i/java/lang/Class.html i/java/lang/Class.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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