Objects First With Java A Practical Introduction Using
Objects First With Java A Practical Introduction Using Blue. J Improving structure with inheritance 1. 1
Main concepts to be covered • • Inheritance Subtyping Substitution Polymorphic variables Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 2
The Do. ME example "Database of Multimedia Entertainment" • stores details about CDs and videos – CD: title, artist, # tracks, playing time, gotit, comment – Video: title, director, playing time, got-it, comment • allows (later) to search for information or print lists Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 3
Do. ME objects Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 4
Do. ME classes Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 5
Do. ME object model Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 6
Class diagram Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 7
public class CD { private String title; private String artist; private String comment; CD source code [ incomplete (comments!) CD(String the. Title, String the. Artist) { title = the. Title; artist = the. Artist; comment = " "; } void set. Comment(String new. Comment) {. . . } ] String get. Comment() {. . . } void print() {. . . } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 8
Video source code [ public class Video { private String title; private String director; private String comment; Video(String the. Title, String the. Direct) { title = the. Title; director = the. Direct; comment = " "; } void set. Comment(String new. Comment) {. . . } ] incomplete (comments!) String get. Comment() {. . . } void print() {. . . } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 9
class Database { private Array. List cds; private Array. List videos; . . . Database source code public void list() { for(Iterator iter = cds. iterator(); iter. has. Next(); ) { CD cd = (CD)iter. next(); cd. print(); System. out. println(); // empty line between items } for(Iterator iter = videos. iterator(); iter. has. Next(); ) { Video video = (Video)iter. next(); video. print(); System. out. println(); // empty line between items } } } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 10
Critique of Do. ME • code duplication – CD and Video 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 Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 11
Using inheritance Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 12
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 Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 13
Inheritance hierarchies Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 14
Inheritance in Java public class Item {. . . } public class CD extends Item {. . . } no change here public class Video extends Item {. . . } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 15
Superclass public class Item { private String title; private int playing. Time; private boolean got. It; private String comment; // constructors and methods omitted. } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 16
Subclasses public class CD extends Item { private String artist; private int number. Of. Tracks; // constructors and methods omitted. } public class Video extends Item { private String director; // constructors and methods omitted. } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 17
Inheritance and constructors public class Item { private String title; private int playing. Time; private boolean got. It; private String comment; /** * Initialise the fields of the item. */ public Item(String the. Title, int time) { title = the. Title; playing. Time = time; got. It = false; comment = ""; } // methods omitted } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 18
Inheritance and public class CD extends Item { constructors private String artist; private int number. Of. Tracks; /** * Constructor for objects of class CD */ public CD(String the. Title, String the. Artist, int tracks, int time) { super(the. Title, time); artist = the. Artist; number. Of. Tracks = tracks; } // methods omitted } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 19
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. Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 20
Adding more item types Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 21
Deeper hierarchies Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 22
Review (so far) Inheritance (so far) helps with: • Avoiding code duplication • Code reuse • Easier maintenance • Extendibility Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 23
public class Database { private Array. List items; New Database source code /** * Construct an empty Database. */ public Database() { items = new Array. List(); } avoids code duplication in client! /** * Add an item to the database. */ public void add. Item(Item the. Item) { items. add(the. Item); }. . . } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 24
New Database source code /** * Print a list of all currently stored CDs and * videos to the text terminal. */ public void list() { for(Iterator iter = items. iterator(); iter. has. Next(); ) { Item item = (Item)iter. next(); item. print(); System. out. println(); // empty line between items } } Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 25
Subtyping First, we had: public void add. CD(CD the. CD) public void add. Video(Video the. Video) Now, we have: public void add. Item(Item the. Item) We call this method with: Video my. Video = new Video(. . . ); database. add. Item(my. Video); Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 26
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. ) Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 27
Subtyping and assignment subclass objects may be assigned to superclass variables Vehicle v 1 = new Vehicle(); Vehicle v 2 = new Car(); Vehicle v 3 = new Bicycle(); Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 28
Subtyping and parameter passing public class Database { public void add. Item(Item the. Item) {. . . } } Video video = new Video(. . . ); CD cd = new CD(. . . ); subclass objects may be passed to superclass parameters database. add. Item(video); database. add. Item(cd); Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 29
Object diagram Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 30
Class diagram Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 31
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. Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 32
The Object class All classes inherit from Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 33
Polymorphic collections • All collections are polymorphic. • The elements are of type Object. public void add(Object element) public Object get(int index) Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 34
Casting revisited • Can assign subtype to supertype. • Cannot assign supertype to subtype! String s 1 = my. List. get(1); error! • Casting fixes this: String s 1 = (String) my. List. get(1); (only if the element really is a String!) Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 35
Wrapper classes • 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? Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 36
Wrapper classes • Simple types (int, char, etc) are not objects. They must be wrapped into an object! • Wrapper classes exist for all simple types: simple type int float char. . . wrapper class Integer Float Character. . . Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 37
Wrapper classes wrap the int value int i = 18; Integer iwrap = new Integer(i); add the wrapper my. Collecton. add(iwrap); . . . Integer element = (Integer) my. Collection. get(0); int value = element. int. Value() retrieve the wrapper unwrap Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 38
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). Objects First with Java - A Practical Introduction using Blue. J, © David J. Barnes, Michael Kölling 39
- Slides: 39