(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 14 Graph class design John Keyser’s Modifications of Slides by Bjarne Stroustrup
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
1 CSC 221: Computer Programming I Fall 2004 Objects and classes: a first pass  software objects, classes, object-oriented design  BlueJ IDE, compilation.
Object Oriented Software Development
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
BIM211 – Visual Programming Objects, Collections, and Events 1.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
(c) University of Washington01-1 CSC 143 Java Programming as Modeling Reading: Ch. 1-6.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
1 CSC 221: Computer Programming I Spring 2008 Objects and classes: a broad view  software objects, classes, object-oriented design  BlueJ IDE, compilation.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Game Maker – Getting Started What is Game Maker?.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Salman Marvasti Sharif University of Technology Winter 2015.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Example: Card Game Create a class called “Card”
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
What is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the.
Week 6 Object-Oriented Programming (2): Polymorphism
Objects First with Java A Practical Introduction using BlueJ
Based on slides by Alyssa Harding & Marty Stepp
Presentation transcript:

(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)

(c) University of Washington02-2 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog eats food Man holds briefcase Dog bites man

(c) University of Washington02-3 Common Relationship Patterns A few types of relationships occur extremely often IS-A : Jill is a student (and an employee and a sister and a skier and.... HAS-A : An airplane has seats (and lights and wings and engines and... These are so important and common that programming languages have special features to model them Some of these you know (maybe without knowing you know) Some of them we’ll learn about in this course, starting now, with inheritance.

(c) University of Washington02-4 Student Jill is-a Airplane has-a wingsseats

(c) University of Washington02-5 Reminder: State vs Behavior State has blue hair wearing glasses wearing blue shoes is hopping mad Behavior clenches fist raises arm hops up and down screams

(c) University of Washington02-6 Inheritance and Interfaces Inheritance is the way that many OO languages model the IS-A relationship Interfaces (in Java) is one special form of inheritance Inheritance is one of the last missing pieces in our knowledge of Java fundamentals A Java Interface declares a set of method signatures i.e., says what behavior exists Does not say how the behavior is implemented i.e., does not give code for the methods Does not describe any state

(c) University of Washington02-7 interface I method signatures of I, without code; no instance variables B's stuff concrete class C methods of I, including code other methods, instance variables of C

(c) University of Washington02-8 A Domain to Model: Geometric Shapes Say we want to write programs that manipulate geometric shapes and produce graphical output This application domain (the world to model) has: Shapes: Rectangles, Squares Ovals, Circles, Arcs Polygons, Lines, Triangles Images Text Windows Let's build a computer model!

(c) University of Washington02-9 Typical Low-Level Design Process (1) Step 1: think up a class for each kind of "thing" to model GWindow Rectangle (no Square) Oval (no Circle), Arc Polygon, Line, Triangle ImageShape TextShape Step 2: identify the state/properties of each thing Each shape has an x/y position & width & height Most shapes have a color Most shapes have a filled/unfilled flag Each kind of shape has its own particular properties

(c) University of Washington02-10 Process (2) Step 3: identify the actions (behaviors) that each kind of thing can do Each shape can add itself to a window: s.addTo(w) Each shape can remove itself from its window: s.removeFromWindow( ) Each shape can move s.moveTo(x, y) s.moveBy(deltaX, deltaY) Most shapes can have its color changed, or its size changed, or … s.setColor(c) s.resize(newWidth, newHeight) …

(c) University of Washington02-11 Key Observation Many kinds of shapes share common properties and actions How can we take advantage of this? It would be nice not to have to define things over and over. Yet there are differences between the shapes, too.

(c) University of Washington02-12 A Solution: Interfaces Declare common behaviors in a Java interface public interface Shape { public int getX(); public void addTo(GWindow w); … } Create a concrete class for each type of thing that implements this interface Annotate the class definition with "implements shape" public class Rectangle implements Shape { public int getX() { … } … }

(c) University of Washington02-13 Shape interface int getX(); void addTo(GWindow w);...` int getX() { //code for getX } void addTo(GWindow w) { //code for addTo }... Rectangle concrete class other methods, instance variables of Rectangle Interface vs. Concrete Class arrow pointing from concrete class to interface

(c) University of Washington02-14 Implementing Interfaces If a class declaration says "implements I..." It MUST implement every single method of the interface It cannot change anything about the method interfaces A class can implement more than one interface When might this be useful? (Hint: think "modeling") A class that implements an interface is completely free to add other methods, instance variables, etc.

(c) University of Washington02-15 Two Benefits of Interfaces The benefits are real, but may be hard to see until you’ve used the concept in several programs 1. Better model of application domain Humans talk about “shape”s as a general group; the computer model should, too 2. Can write code that works on any concrete object that implements the interface (e.g., on any Shape) Each interface introduces a new type Can declare variables, arguments, results, etc. of that type

(c) University of Washington02-16 Using Interfaces as Types Each interface introduces a new type An object of a concrete class that implements an interface has two types, effectively The concrete type (=the type of the class) The interface type (because of the IS_A relationship between the class and the interface) Such an object can be used in any situation where one or the other type is appropriate As variables (Shape s = new Rectangle();) As arguments and parameters (e.g. in GWindow, public void add(Shape s)) As return types, e.g. public Shape getShape() { return new Rectangle(); }

(c) University of Washington02-17 Some Domains for Examples Another set of domains to model: animations & simulations Example domains, and the things in those domains: Financial simulation: bank accounts, customers, investors Planetary simulation: suns, planets, moons, spaceships, asteroids Fantasy game: characters, monsters, weapons, walls Can have a visual representation of the simulation, using graphical shapes & windows Let's build some computer models!

(c) University of Washington02-18 An Example: A Planetary Simulation Model the motion of celestial bodies Requirements: left a bit vague for this example Step 1: make classes for each kind of thing Step 2: identify the state/properties of each thing Step 3: identify the actions that each kind of thing can do Step 4: if there are classes with many common behaviors, considering making an interface out of the common part

(c) University of Washington02-19 An Example: A Planetary Simulation Step 1: make classes for each kind of thing Sun, Planet, Spaceship Universe containing it all Step 2: identify the state/properties of each thing Location, speed, mass List of things in the universe Step 3: identify the actions that each kind of thing can do Compute force exerted by other things; update position & velocity based on forces; display itself on a window Tell each thing in universe to update itself based on all other things; react to keyboard & mouse inputs

(c) University of Washington02-20 Frameworks When a recurring pattern of classes is identified, it can be extracted into a framework Often use interfaces in place of particular classes (e.g. CelestialBody) Clients then build their models by extending the framework Making instances of framework classes (e.g. Universe) Making application-specific classes that implement framework interfaces (e.g. CelestialBody) Making new application-specific classes Libraries are simple kinds of frameworks Don't have interfaces for clients to implement