fen 2006-11-15introjava2006 aau1 nedarvning specialisering/generalisering subklasse/superklasse...

33
FEN 2006-11-15 IntroJava2006 AAU 1 Nedarvning Specialisering/ Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

Upload: penelope-bakehouse

Post on 01-Apr-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 1

Nedarvning

Specialisering/GeneraliseringSubklasse/SuperklasseStatisk/Dynamisk type

Polymorfi

Interfaces

Page 2: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 2

Dome Eksempel

• Lagrer information om videoer og CD-er:

• Masser af kode er ens i de to klasser

Page 3: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 3

Nedarvning

• Fælles egenskaber (attributter og metoder) defineres på superklassen Item

• Subklasserne CD og Video arver alle egenskaber fra superklassen

Angiver arv

Page 4: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 4

Nedarvning kan give store hierarkier:

Page 5: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 5

Inheritance: Dome Example

Page 6: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 6

Inheritance in Java

public class Item{ ...}

public class CD extends Item{ ...}

public class Video extends Item { ...}

no change here

change here

Page 7: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 7

Superclass

public class Item{ private String title; private int playingTime; private boolean gotIt; private String comment;

// constructors and methods omitted.}

Page 8: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 8

Subclasses

public class CD extends Item{ private String artist; private int numberOfTracks;

// constructors and methods omitted.}

public class Video extends Item { private String director;

// constructors and methods omitted.}

Page 9: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 9

public class Item{ private String title; private int playingTime; private boolean gotIt; private String comment;

/** * Initialise the fields of the item. */ public Item(String theTitle, int time) { title = theTitle; playingTime = time; gotIt = false; comment = ""; }

// methods omitted}

Inheritance and constructors

Page 10: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 10

Inheritance and constructors

public class CD extends Item{ private String artist; private int numberOfTracks;

/** * Constructor for objects of class CD */ public CD(String theTitle, String theArtist, int tracks, int time) { super(theTitle, time); artist = theArtist; numberOfTracks = tracks; }

// methods omitted}

Page 11: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 11

More subtypes and deeper hierarchies

Page 12: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 12

public class Database{ private ArrayList<Item> items;

/** * Construct an empty Database. */ public Database() { items = new ArrayList<Item>(); }

/** * Add an item to the database. */ public void addItem(Item theItem) { items.add(theItem); } ...}

Database source code

avoids code duplication in client!

Page 13: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 13

/** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for(int i= 0; i<items.size(); i++) { Item item = items.get(i); item.print(); System.out.println(); // empty line between items }}

Database source code

Page 14: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 14

Review (so far)

Inheritance (so far) helps with:

• Avoiding code duplication

• Code reuse

• Easier maintenance

• Extendibility

Page 15: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 15

Subtyping

We have: public void addItem(Item theItem)

We call this method with: Video myVideo = new Video(...); database.addItem(myVideo);

Static type

Dynamic type

Page 16: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 16

Subtyping and Assignment

Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle();

subclass objects may be assigned to superclass variables

Static type

Dynamic type

Page 17: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 17

Subtyping and parameter passing

public class Database{ public void addItem(Item theItem) { ... }}

Video video = new Video(...);CD cd = new CD(...);

database.addItem(video);database.addItem(cd);

subclass objects may be passed to superclass parameters

Dynamic type

Static type

Page 18: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 18

Object diagram

Page 19: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 19

Polymorphic variables

• Object variables in Java are polymorphic.

(They can hold objects of more than one type.)

• They can hold objects of the declared type, or of subtypes of the declared type.

Page 20: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 20

The inheritance hierarchy

Print()-method only defined in Item

Page 21: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 21

Conflicting output

CD: A Swingin' Affair (64 mins)* Frank Sinatra tracks: 16 my favourite Sinatra album video: O Brother, Where Art Thou? (106 mins) Joel & Ethan Coen The Coen brothers’ best movie!

title: A Swingin' Affair (64 mins)* my favourite Sinatra album

title: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie!

What we want

What we now have

Page 22: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 22

print()-method in Class Item public class Item{ private String title; private int playingTime; private boolean gotIt; private String comment; // Print details of this item to the text terminal public void print() { System.out.print(title + " (" + playingTime + " mins)"); if(gotIt) { System.out.println("*"); } else { System.out.println(); } System.out.println(" " + comment); }}

Can of course only print it’s own

fields

Page 23: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 23

Overriding: the solution

print method in both super- and subclasses.

Satisfies both static

and dynamic

type checking.

Page 24: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 24

Method lookup – 1st version

No inheritance or polymorphism.The obvious method is selected.

Page 25: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 25

Method lookup – 2nd verison

Inheritance but no overriding. The

inheritance hierarchy is ascended, searching for

a match.

Page 26: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 26

Method lookup – 3rd version

Polymorphism and overriding. The

‘first’ version found is used.

Page 27: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 27

Calling an overridden method

public class CD extends Item{ ... public void print() { super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); } ...}

Page 28: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 28

Another example: Banking

• The bank has customers and accounts

• A customer has a collection of accounts

• The bank has standard accounts and budget accounts where an overdraft is allowed

• See the project: bankExample

Page 29: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 29

Interfaces• Et interface er en klasse helt uden implementation, dvs.:

– ingen attributter

– ingen metodeimplementeringer

– kun metodehoveder

public interface ItemIF{ public String getTitle(); public void setComment(String comment); public String getComment(); public void setOwn(boolean ownIt); public boolean getOwn(); public void print();}

Page 30: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 30

Hvorfor interfaces?

• Adskiller specifikation (hvad?) fra implementation (hvordan?)

• Kan bruges som statisk type, dvs. klientprogrammet behøver ikke kende implementationen (klassen)

• Klasser kan kun arve fra en klasse, men implementere flere interfaces

• Det er nødvendigt at kende til interfaces, da de bruges i udstrakt grad i Java’s biblioteker

Page 31: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 31

Dome v. 2:

Interface

Implementerer

Arver/extends

Page 32: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 32

Implementering af et interface

public abstract class Item implements ItemIF{ //attributter og constructor

//evt yderligere metoder }

public String getTitle(){…}; public void setComment(String comment){…}; public String getComment(){…}; public void setOwn(boolean ownIt){…}; public boolean getOwn(){…}; public void print(){…};

Metoder defineret i

interfacet skal implementeres.

Page 33: FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces

FEN 2006-11-15 IntroJava2006 AAU 33

Interface som statisk type

public class Database{ private ArrayList<ItemIF> items; public Database(){ items = new ArrayList<ItemIF>(); } public void addItem(ItemIF theItem){…} public void list(){ for(int i= 0; i<items.size(); i++) { ItemIF item = items.get(i); item.print(); } }}

Alle klasser, som implementerer

ItemIF kan bruges som dynamisk type