Download - Chapter08
![Page 1: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/1.jpg)
Objects First With JavaA Practical Introduction Using BlueJ
Improving structure with inheritance
1.1
![Page 2: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/2.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2
Main concepts to be covered
• Inheritance• Subtyping• Substitution• Polymorphic variables
![Page 3: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/3.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 3
The DoME example
"Database of Multimedia Entertainment"
• stores details about CDs and videos– CD: title, artist, # tracks, playing time, got-it,
comment– Video: title, director, playing time, got-it,
comment
• allows (later) to search for information or print lists
![Page 4: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/4.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 4
DoME objects
![Page 5: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/5.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 5
DoME classes
![Page 6: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/6.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 6
DoME object model
![Page 7: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/7.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 7
Class diagram
![Page 8: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/8.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 8
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!)[ ]
![Page 9: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/9.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 9
Video source code
public class Video { private String title; private String director; private String comment;
Video(String theTitle, String theDirect) {
title = theTitle;director = theDirect;comment = " ";
}
void setComment(String newComment) { ... }
String getComment() { ... }
void print() { ... } ...}
incomplete(comments!)[ ]
![Page 10: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/10.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 10
class Database {
private ArrayList cds; private ArrayList videos; ...
public void list() { for(Iterator iter = cds.iterator(); iter.hasNext(); ) { CD cd = (CD)iter.next(); cd.print(); System.out.println(); // empty line between items }
for(Iterator iter = videos.iterator(); iter.hasNext(); ) { Video video = (Video)iter.next(); video.print(); System.out.println(); // empty line between items } }}
Database source code
![Page 11: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/11.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 11
Critique of DoME
• 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
![Page 12: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/12.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 12
Using inheritance
![Page 13: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/13.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 13
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
![Page 14: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/14.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 14
Inheritance hierarchies
![Page 15: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/15.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 15
Inheritance in Java
public class Item{ ...}
public class CD extends Item{ ...}
public class Video extends Item { ...}
no change here
change here
![Page 16: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/16.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 16
Superclass
public class Item{ private String title; private int playingTime; private boolean gotIt; private String comment;
// constructors and methods omitted.}
![Page 17: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/17.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 17
Subclassespublic 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 18: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/18.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 18
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 19: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/19.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 19
Inheritance and
constructorspublic 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 20: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/20.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 20
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.
![Page 21: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/21.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 21
Adding more item types
![Page 22: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/22.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 22
Deeper hierarchies
![Page 23: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/23.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 23
Review (so far)
Inheritance (so far) helps with:• Avoiding code duplication• Code reuse• Easier maintenance• Extendibility
![Page 24: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/24.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 24
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!
![Page 25: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/25.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 25
/** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for(Iterator iter = items.iterator(); iter.hasNext(); ) { Item item = (Item)iter.next(); item.print(); System.out.println(); // empty line between items }}
New Database source code
![Page 26: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/26.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 26
Subtyping
First, we had: public void addCD(CD theCD) public void addVideo(Video theVideo)
Now, we have: public void addItem(Item theItem)
We call this method with: Video myVideo = new Video(...); database.addItem(myVideo);
![Page 27: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/27.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 27
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 .)
![Page 28: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/28.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 28
Subtyping and assignment
Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle();
subclass objects may be assigned to superclass variables
![Page 29: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/29.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 29
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
![Page 30: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/30.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 30
Object diagram
![Page 31: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/31.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 31
Class diagram
![Page 32: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/32.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 32
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 33: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/33.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 33
The Object class
All classes inherit from Object.
![Page 34: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/34.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 34
Polymorphic collections
• All collections are polymorphic.• The elements are of type Object.
public void add(Object element)
public Object get(int index)
![Page 35: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/35.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 35
Casting revisited
• Can assign subtype to supertype.• Cannot assign supertype to subtype!
String s1 = myList.get(1); error!
• Casting fixes this:
String s1 = (String) myList.get(1);
(only if the element really is a String!)
![Page 36: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/36.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 36
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?
![Page 37: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/37.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 37
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 wrapper classint Integerfloat Floatchar Character... ...
![Page 38: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/38.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 38
Wrapper classes
int i = 18; Integer iwrap = new Integer(i);
myCollecton.add(iwrap);...
Integer element = (Integer) myCollection.get(0);int value = element.intValue()
wrap the int value
add the wrapper
retrieve the wrapper
unwrap
![Page 39: Chapter08](https://reader033.vdocument.in/reader033/viewer/2022061111/545532e4af795994188b4777/html5/thumbnails/39.jpg)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 39
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).