Top Banner
Improving structure Improving structure with inheritance with inheritance
40

Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

Improving structure with Improving structure with inheritanceinheritance

Page 2: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 2

Main concepts to be coveredMain concepts to be covered

InheritanceSubtypingSubstitutionPolymorphic variables

Page 3: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 3

The DoME exampleThe 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,

commentallows (later) to search for information or

print lists

Page 4: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 4

DoME objectsDoME objects

Page 5: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 5

DoME classesDoME classes

Page 6: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 6

DoME object modelDoME object model

Page 7: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 7

Class diagramClass diagram

Page 8: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 8

CD CD source source codecode

public class CD { private String title; private String artist; private String comment;

public CD(String theTitle, String theArtist) {

title = theTitle;artist = theArtist;comment = " ";

}

void setComment(String newComment) { ... }

String getComment() { ... }

void print() { ... } ...}

incomplete(comments

!)[ ]

Page 9: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 9

Video Video source source codecode

public class Video { private String title; private String director; private String comment;

public Video(String theTitle, String theDirect) {

title = theTitle;director = theDirect;comment = " ";

}

void setComment(String newComment) { ... }

String getComment() { ... }

void print() { ... } ...}

incomplete(comments!)[ ]

Page 10: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 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 Database source codesource code

Page 11: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 11

Critique of DoMECritique 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 12

Using inheritanceUsing inheritance

Page 13: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 13

Using inheritanceUsing inheritance

define one superclass : Itemdefine subclasses for Video and CDthe superclass defines common attributesthe subclasses inherit the superclass

attributesthe subclasses add own attributes

Page 14: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 14

Inheritance hierarchiesInheritance hierarchies

Page 15: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 15

Inheritance in JavaInheritance in Java

public class Item{ ...}

public class CD extends Item{ ...}

public class Video extends Item { ...}

no change here

change here

Page 16: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 16

SuperclassSuperclass

public class Item{ private String title; private int playingTime; private boolean gotIt; private String comment;

// constructors and methods omitted.}

Page 17: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 17

SubclassesSubclassespublic 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 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 Inheritance and constructorsconstructors

Page 19: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 19

Inheritance and Inheritance and constructorsconstructors

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}

Page 20: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 20

Superclass constructor callSuperclass 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 21

Adding more item typesAdding more item types

Page 22: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 22

Deeper hierarchiesDeeper hierarchies

Page 23: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 23

Review (so far)Review (so far)

Inheritance (so far) helps with:Avoiding code duplicationCode reuseEasier maintenanceExtendibility

Page 24: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 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 New Database source codesource code

avoids code duplication in client!

Page 25: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 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 codeNew Database source code

Page 26: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 26

SubtypingSubtyping

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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 27

Subclasses and subtypingSubclasses 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 28

Subtyping and assignmentSubtyping and assignment

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

subclass objects may be assigned to superclass variables

Page 29: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 29

Subtyping and parameter Subtyping and parameter passingpassing

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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 30

Object diagramObject diagram

Page 31: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 31

Class diagramClass diagram

Page 32: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 32

Polymorphic variablesPolymorphic 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 33

The Object classThe Object class

All classes inherit from Object.

Page 34: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 34

Polymorphic collectionsPolymorphic collections

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

public void add(Object element)

public Object get(int index)

Page 35: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 35

Casting revisitedCasting 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 36

Wrapper classesWrapper 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 37

Wrapper classesWrapper 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 38

Wrapper classesWrapper 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: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 39

ReviewReview

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

Page 40: Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.

25/11/2004 Lecture 7: Inheritance 40

ConceptsConcepts

Inheritance Superclass Subclass Base Class

Object

Subtyping Type casting Polymorphic variables

Inheritance Hierarchies