Improving structure with inheritance 2.0 Main concepts to be - - PowerPoint PPT Presentation

improving structure with inheritance
SMART_READER_LITE
LIVE PREVIEW

Improving structure with inheritance 2.0 Main concepts to be - - PowerPoint PPT Presentation

Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java - A Practical


slide-1
SLIDE 1

Objects First With Java A Practical Introduction Using BlueJ

Improving structure with inheritance

2.0

slide-2
SLIDE 2

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

2

Main concepts to be covered

  • Inheritance
  • Subtyping
  • Substitution
  • Polymorphic variables
slide-3
SLIDE 3

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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 make additions or to

search for information or to print lists

slide-4
SLIDE 4

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

4

DoME objects

slide-5
SLIDE 5

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

5

DoME classes

(UML Syntax)

accessor and mutator methods for varying fields (gotIt, comment) the other fields are set in the constructor

slide-6
SLIDE 6

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

6

DoME object model

now the database object, holding two collection objects

slide-7
SLIDE 7

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

7

UML Class diagram

Collection is omitted

slide-8
SLIDE 8

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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!)

[ ]

slide-9
SLIDE 9

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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!)

[ ]

very similar to CD!!

slide-10
SLIDE 10

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

10

class Database { private ArrayList<CD> cds; private ArrayList<Video> videos; ... public void list() { for(Iterator iter = cds.iterator(); iter.hasNext(); ) { CD cd = iter.next(); cd.print(); System.out.println(); // empty line between items } for(Iterator iter = videos.iterator(); iter.hasNext(); ) { Video video = iter.next(); video.print(); System.out.println(); // empty line between items } } }

Database source code

prints a list of all CDs and videos

slide-11
SLIDE 11

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

11

Critique of DoME

  • code duplication

– CD and Video classes very similar (large parts are identical) – makes maintenance difficult/more work – introduces danger of bugs through incorrect maintenance

  • code duplication also in Database class

– Imagine a third media “VideoGame” – what has to be done?

slide-12
SLIDE 12

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

12

DoME Inheritance

Class Diagram

Superclass Subclass

slide-13
SLIDE 13

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

13

Inheritance

  • define one superclass for Item
  • define subclasses for Video and CD
  • the superclass defines common attributes

(fields and methods)

  • the subclasses inherit from or extend the

superclass

  • the subclasses inherit the superclass

attributes

  • the subclasses add their own attributes
slide-14
SLIDE 14

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

14

Inheritance in Java

public class Item { ... } public class CD extends Item { ... } public class Video extends Item { ... }

slide-15
SLIDE 15

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

15

Superclass

public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }

  • bject generation possible, but usually not intended

Attention! private fields are not visible to the subclass!

slide-16
SLIDE 16

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

16

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. }

slide-17
SLIDE 17

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

17

Inheritance hierarchy

slide-18
SLIDE 18

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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

slide-19
SLIDE 19

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

19

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 } privacy also applies between subclasses and their superclass

slide-20
SLIDE 20

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

20

Superclass constructor call

  • Subclass constructors must always

contain a 'super' call.

  • If none is written, the compiler inserts
  • ne (without parameters)

– works only if the superclass has a constructor without parameters

  • Must be the first statement in the

subclass constructor.

slide-21
SLIDE 21

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

21

Adding more item types

example of code reuse!

slide-22
SLIDE 22

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

22

Deeper hierarchies

slide-23
SLIDE 23

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

23

Review (so far)

Inheritance helps with:

  • Avoiding code duplication
  • Easier maintenance
  • Extendibility
slide-24
SLIDE 24

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

24

public class Database { private ArrayList<Item> items; /** * Construct an empty Database. */ public Database() { items = new ArrayList<Item>(); } /** * Add an item to the database. */ public void addItem(Item theItem) { items.add(theItem); } ... }

New Database

avoids code duplication

slide-25
SLIDE 25

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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

slide-26
SLIDE 26

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

26

Subtyping

First, we had:

public void addCD(CD theCD) public void addVideo(Video theVideo)

Now, we have:

public void addItem(Item theItem)

Method call:

Video myVideo = new Video(...); database.addItem(myVideo);

slide-27
SLIDE 27

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

27

Subclasses and Subtyping

  • Classes define types.
  • Subclasses define subtypes.
  • Objects of subclasses can be used

where objects of supertypes are required (interface ↔ instance). This is called substitution.

slide-28
SLIDE 28

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

28

Subtyping and assignment

Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Vehicle v3 = new Bicycle();

subclass objects may be assigned to superclass variables – but not the other way round!

slide-29
SLIDE 29

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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 – but not the other way round!

slide-30
SLIDE 30

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

30

Object diagram

slide-31
SLIDE 31

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

31

Class diagram

slide-32
SLIDE 32

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

32

Polymorphic variables

  • Object: a superclass for all objects
  • 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
SLIDE 33

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

33

The Object class

All classes inherit from Object.

slide-34
SLIDE 34

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

34

Polymorphic collections

  • All collections are polymorphic.
  • The elements are of type Object.

public void add(Object element) public Object get(int index)

slide-35
SLIDE 35

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

35

Casting revisited

  • Can assign subtype to supertype.
  • Cannot assign supertype to subtype!

Video v1 = myList.get(1); error!

  • Casting fixes this:

Video v1 = (Video) myList.get(1);

  • nly if the element has type Video – otherwise runtime

error

slide-36
SLIDE 36

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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?
slide-37
SLIDE 37

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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 class int Integer float Float char Character ... ...

slide-38
SLIDE 38

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

38

Wrapper classes

Collection<Object> myCollection; myCollection = new ArrayList<Object>(); 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

slide-39
SLIDE 39

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

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
  • bjects are expected (substitution).