FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces
FEN IntroJava2006 AAU2 Dome Eksempel Lagrer information om videoer og CD-er: Masser af kode er ens i de to klasser
FEN IntroJava2006 AAU3 Nedarvning Fælles egenskaber (attributter og metoder) defineres på superklassen Item Subklasserne CD og Video arver alle egenskaber fra superklassen Angiver arv
FEN IntroJava2006 AAU4 Nedarvning kan give store hierarkier:
FEN IntroJava2006 AAU5 Inheritance: Dome Example
FEN IntroJava2006 AAU6 Inheritance in Java public class Item {... } public class CD extends Item {... } public class Video extends Item {... } no change here change here
FEN IntroJava2006 AAU7 Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }
FEN IntroJava2006 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. }
FEN IntroJava2006 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
FEN IntroJava2006 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 }
FEN IntroJava2006 AAU11 More subtypes and deeper hierarchies
FEN IntroJava2006 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!
FEN IntroJava2006 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
FEN IntroJava2006 AAU14 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility
FEN IntroJava2006 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
FEN IntroJava2006 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
FEN IntroJava2006 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
FEN IntroJava2006 AAU18 Object diagram
FEN IntroJava2006 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.
FEN IntroJava2006 AAU20 The inheritance hierarchy Print()-method only defined in Item
FEN IntroJava2006 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
FEN IntroJava2006 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
FEN IntroJava2006 AAU23 Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking.
FEN IntroJava2006 AAU24 Method lookup – 1st version No inheritance or polymorphism. The obvious method is selected.
FEN IntroJava2006 AAU25 Method lookup – 2nd verison Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match.
FEN IntroJava2006 AAU26 Method lookup – 3rd version Polymorphism and overriding. The ‘first’ version found is used.
FEN IntroJava2006 AAU27 Calling an overridden method public class CD extends Item {... public void print() { super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); }... }
FEN IntroJava2006 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
FEN IntroJava2006 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();}
FEN IntroJava2006 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
FEN IntroJava2006 AAU31 Dome v. 2: Interface Implementerer Arver/extends
FEN IntroJava2006 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.
FEN IntroJava2006 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