Further abstraction techniques

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Fields, Constructors, Methods
Further abstraction techniques Abstract classes and interfaces 5.0.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Improving structure with inheritance
Further abstraction techniques Abstract classes and interfaces 1.0.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Grouping Objects 3 Iterators, collections and the while loop.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.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.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Further abstraction techniques Abstract classes and interfaces 3.0.
Abstract Classes and Interfaces
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Understanding class definitions
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 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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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)
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Further abstraction techniques Abstract classes and interfaces 6.0.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
11 Further abstraction techniques
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Objects and Classes CITS1001 week 1.
Objects First with Java A Practical Introduction using BlueJ
Inheritance and Polymorphism
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
COS 260 DAY 2 Tony Gauvin.
Advanced Java Programming
Creating cooperating objects
COS 260 DAY 19 Tony Gauvin.
Java Inheritance.
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
Improving structure with inheritance
Presentation transcript:

Further abstraction techniques Abstract classes and interfaces 5.0

Main concepts to be covered Objects First with Java 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 © David J. Barnes and Michael Kölling

Simulations Programs regularly used to simulate real-world activities. city traffic the weather nuclear processes stock market fluctuations environmental changes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Simulations They are often only partial simulations. They often involve simplifications. Greater detail has the potential to provide greater accuracy. Greater detail typically requires more resource. Processing power. Simulation time. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Benefits of simulations Support useful prediction. The weather. Allow experimentation. Safer, cheaper, quicker. Example: ‘How will the wildlife be affected if we cut a highway through the middle of this national park?’ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Predator-prey simulations There is often a delicate balance between species. A lot of prey means a lot of food. A lot of food encourages higher predator numbers. More predators eat more prey. Less prey means less food. Less food means ... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

The foxes-and-rabbits project Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Main classes of interest Fox Simple model of a type of predator. Rabbit Simple model of a type of prey. Simulator Manages the overall simulation task. Holds a collection of foxes and rabbits. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

The remaining classes Field Location Represents a 2D field. Location Represents a 2D position. SimulatorView, FieldStats, Counter Maintain statistics and present a view of the field. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

A Rabbit’s state public class Rabbit { Static fields omitted. // Individual characteristics (instance fields). // The rabbit's age. private int age; // Whether the rabbit is alive or not. private boolean alive; // The rabbit's position private Location location; // The field occupied private Field field; Methods omitted. } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

A Rabbit’s behavior Managed from the run method. Age incremented at each simulation ‘step’. A rabbit could die at this point. Rabbits that are old enough might breed at each step. New rabbits could be born at this point. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Rabbit simplifications Rabbits do not have different genders. In effect, all are female. The same rabbit could breed at every step. All rabbits die at the same age. Others? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

A Fox’s state public class Fox { Static fields omitted {  Static fields omitted   // The fox's age. private int age; // Whether the fox is alive or not. private boolean alive; // The fox's position private Location location; // The field occupied private Field field; // The fox's food level, which is increased // by eating rabbits. private int foodLevel; Methods omitted. } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

A Fox’s behavior Managed from the hunt method. Foxes also age and breed. They become hungry. They hunt for food in adjacent locations. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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 Field objects are used: field and updatedField. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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(); Fox fox = it.next(); fox.hunt(newFoxes); if(! fox.isAlive()) { Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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

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

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

Abstract classes and methods Abstract methods have abstract in the signature. Abstract methods have no body. 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

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

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

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

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

An Actor interface public interface Actor { /**   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

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

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

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

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

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

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

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

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

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