Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces."— Presentation transcript:

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

2 FEN 2006-11-15IntroJava2006 AAU2 Dome Eksempel Lagrer information om videoer og CD-er: Masser af kode er ens i de to klasser

3 FEN 2006-11-15IntroJava2006 AAU3 Nedarvning Fælles egenskaber (attributter og metoder) defineres på superklassen Item Subklasserne CD og Video arver alle egenskaber fra superklassen Angiver arv

4 FEN 2006-11-15IntroJava2006 AAU4 Nedarvning kan give store hierarkier:

5 FEN 2006-11-15IntroJava2006 AAU5 Inheritance: Dome Example

6 FEN 2006-11-15IntroJava2006 AAU6 Inheritance in Java public class Item {... } public class CD extends Item {... } public class Video extends Item {... } no change here change here

7 FEN 2006-11-15IntroJava2006 AAU7 Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }

8 FEN 2006-11-15IntroJava2006 AAU8 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. }

9 FEN 2006-11-15IntroJava2006 AAU9 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

10 FEN 2006-11-15IntroJava2006 AAU10 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 }

11 FEN 2006-11-15IntroJava2006 AAU11 More subtypes and deeper hierarchies

12 FEN 2006-11-15IntroJava2006 AAU12 public class Database{ private ArrayList items; /** * Construct an empty Database. */ public Database() { items = new ArrayList (); } /** * Add an item to the database. */ public void addItem(Item theItem) { items.add(theItem); }...} Database source code avoids code duplication in client!

13 FEN 2006-11-15IntroJava2006 AAU13 /** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for( int i= 0 ; i< ite ms.size() ; i++ ) { Item item = items.get(i) ; item.print(); System.out.println(); // empty line between items } Database source code

14 FEN 2006-11-15IntroJava2006 AAU14 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility

15 FEN 2006-11-15IntroJava2006 AAU15 Subtyping We have: public void addItem(Item theItem) We call this method with: Video myVideo = new Video(...); database.addItem(myVideo); Static type Dynamic type

16 FEN 2006-11-15IntroJava2006 AAU16 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

17 FEN 2006-11-15IntroJava2006 AAU17 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

18 FEN 2006-11-15IntroJava2006 AAU18 Object diagram

19 FEN 2006-11-15IntroJava2006 AAU19 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.

20 FEN 2006-11-15IntroJava2006 AAU20 The inheritance hierarchy Print()-method only defined in Item

21 FEN 2006-11-15IntroJava2006 AAU21 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

22 FEN 2006-11-15IntroJava2006 AAU22 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

23 FEN 2006-11-15IntroJava2006 AAU23 Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking.

24 FEN 2006-11-15IntroJava2006 AAU24 Method lookup – 1st version No inheritance or polymorphism. The obvious method is selected.

25 FEN 2006-11-15IntroJava2006 AAU25 Method lookup – 2nd verison Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match.

26 FEN 2006-11-15IntroJava2006 AAU26 Method lookup – 3rd version Polymorphism and overriding. The ‘first’ version found is used.

27 FEN 2006-11-15IntroJava2006 AAU27 Calling an overridden method public class CD extends Item {... public void print() { super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); }... }

28 FEN 2006-11-15IntroJava2006 AAU28 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: bankExamplebankExample

29 FEN 2006-11-15IntroJava2006 AAU29 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();}

30 FEN 2006-11-15IntroJava2006 AAU30 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

31 FEN 2006-11-15IntroJava2006 AAU31 Dome v. 2: Interface Implementerer Arver/extends

32 FEN 2006-11-15IntroJava2006 AAU32 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.

33 FEN 2006-11-15IntroJava2006 AAU33 Interface som statisk type public class Database{ private ArrayList items; public Database(){ items = new ArrayList (); } 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


Download ppt "FEN 2006-11-15IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces."

Similar presentations


Ads by Google