improving structure with inheritance 3.0. 2 objects first with java - a practical introduction using...

Download Improving structure with inheritance 3.0. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts

If you can't read please download the document

Upload: chastity-murphy

Post on 16-Dec-2015

220 views

Category:

Documents


5 download

TRANSCRIPT

  • Slide 1
  • Improving structure with inheritance 3.0
  • Slide 2
  • 2 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables
  • Slide 3
  • 3 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling The DoME example "Database of Multimedia Entertainment" stores details about CDs and DVDs CD: title, artist, # tracks, playing time, got- it, comment DVD: title, director, playing time, got-it, comment allows (later) to search for information or print lists
  • Slide 4
  • 4 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling DoME objects
  • Slide 5
  • 5 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling DoME classes top half shows fields bottom half shows methods
  • Slide 6
  • 6 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling DoME object model
  • Slide 7
  • 7 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Class diagram
  • Slide 8
  • 8 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling CD source code public class CD{ private String title; private String artist; private String comment; CD(String theTitle, String theArtist) { title = theTitle;artist = theArtist;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...} incomplete (comments!) [ ]
  • Slide 9
  • 9 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling DVD source code public class DVD{ private String title; private String director; private String comment; DVD(String theTitle, String theDirector) { title = theTitle;director = theDirector;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...} incomplete (comments!) [ ]
  • Slide 10
  • 10 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling class Database { private ArrayList cds ; private ArrayList dvds ;... public void list() { for( CD cd : cds ) { cd.print(); System.out.println(); // empty line between items } for( DVD dvd : dvds ) { dvd.print(); System.out.println(); // empty line between items } Database source code
  • Slide 11
  • 11 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Critique of DoME code duplication CD and DVD classes very similar (large part are identical) makes maintenance difficult/more work introduces danger of bugs through incorrect maintenance code duplication also in Database class
  • Slide 12
  • 12 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Using inheritance
  • Slide 13
  • 13 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Using inheritance define one superclass : Item define subclasses for Video and CD the superclass defines common attributes the subclasses inherit the superclass attributes the subclasses add own attributes
  • Slide 14
  • 14 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Inheritance hierarchies
  • Slide 15
  • 15 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Inheritance in Java public class Item {... } public class CD extends Item {... } public class DVD extends Item {... } no change here change here
  • Slide 16
  • 16 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }
  • Slide 17
  • 17 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Subclasses public class CD extends Item { private String artist; private int numberOfTracks; // constructors and methods omitted. } public class DVD extends Item { private String director; // constructors and methods omitted. }
  • Slide 18
  • 18 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling 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
  • Slide 19
  • 19 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling 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 }
  • Slide 20
  • 20 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Superclass constructor call Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) works only, if the superclass has a constructor without parameters Must be the first statement in the subclass constructor.
  • Slide 21
  • 21 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Adding more item types
  • Slide 22
  • 22 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Deeper hierarchies
  • Slide 23
  • 23 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility
  • Slide 24
  • 24 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling 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); }...} New Database source code avoids code duplication in client!
  • Slide 25
  • 25 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling /** * Print a list of all currently stored CDs and * DVD s to the text terminal. */public void list(){ for( Item item : items ) { item.print(); // Print an e mpty line between items System.out.println(); } New Database source code
  • Slide 26
  • 26 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Subtyping First, we had: public void addCD(CD theCD) public void addVideo( DVD the DVD ) Now, we have: public void addItem(Item theItem) We call this method with: DVD my DVD = new DVD (...); database.addItem(my DVD );
  • Slide 27
  • 27 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Subclasses and subtyping Classes define types. Subclasses define subtypes. Objects of subclasses can be used where objects of supertypes are required. (This is called substitution.)
  • Slide 28
  • 28 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Subtyping and assignment Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables
  • Slide 29
  • 29 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Subtyping and parameter passing public class Database { public void addItem(Item theItem) {... } DVD dvd = new DVD(...); CD cd = new CD(...); database.addItem(dvd); database.addItem(cd); subclass objects may be passed to superclass parameters
  • Slide 30
  • 30 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Object diagram
  • Slide 31
  • 31 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Class diagram
  • Slide 32
  • 32 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling 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.
  • Slide 33
  • 33 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Casting Can assign subtype to supertype. Cannot assign supertype to subtype! Vechicle v; Car c = new Car(); v = c; // correct; c = v; compile-time error! Casting fixes this : c = (Car) v; (only ok if the vehicle really is a Car!)
  • Slide 34
  • 34 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Casting An object type in parentheses. Used to overcome 'type loss'. The object is not changed in any way. A runtime check is made to ensure the object really is of that type: ClassCastException if it isn't! Use it sparingly.
  • Slide 35
  • 35 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling The Object class All classes inherit from Object.
  • Slide 36
  • 36 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Polymorphic collections All collections are polymorphic. The elements are of type Object. public void add(Object element) public Object get(int index)
  • Slide 37
  • 37 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Collections and primitive types All objects can be entered into collections...... because collections accept elements of type Object...... and all classes are subtypes of Object. Great! But what about simple types?
  • Slide 38
  • 38 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Wrapper classes Primitive types (int, char, etc) are not objects. They must be wrapped into an object! Wrapper classes exist for all simple types: simple typewrapper class intInteger floatFloat charCharacter...
  • Slide 39
  • 39 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Wrapper classes int i = 18; Integer iwrap = new Integer(i); int value = iwrap.intValue(); wrap the value unwrap it In practice, autoboxing and unboxing mean we don't often have to do this.
  • Slide 40
  • 40 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Autoboxing and unboxing private ArrayList markList; public void storeMark(int mark) { markList.add(mark); } int firstMark = markList.remove(0); autoboxing unboxing
  • Slide 41
  • 41 Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling Review Inheritance allows the definition of classes as extensions of other classes. Inheritance avoids code duplication allows code reuse simplifies the code simplifies maintenance and extending Variables can hold subtype objects. Subtypes can be used wherever supertype objects are expected (substitution).