oo design with inheritance - university of arizona · 2010. 9. 23. · oo design with inheritance c...

33
1 OO Design with Inheritance C Sc 335 Rick Mercer

Upload: others

Post on 01-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

1

OO Design with Inheritance

C Sc 335

Rick Mercer

Page 2: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

2

Justification and Outline

I introduce inheritance different than most books

that just show the mechanics and examples like

the Bicycle hierarchy from Sun

I start with a reason to use inheritance– Show the objects found for a Library System

– Recognize when to use inheritance

– Build an inheritance hierarchy

– Design Guidelines related to inheritance

– See another use of polymorphism

Page 3: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

3

The 3 Pillars of OOP&D

Object-Oriented Programming

– Encapsulation

• Hide details in a class, provide methods

– Polymorphism

• Same name, different behavior, based on type

– Inheritance

• Capture common attributes and behaviors in a

base class and extend it for different types

Page 4: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

4

Object-Oriented Technology

OOT began with Simula 67

– developed in Norway

– acronym for simulation language

Why this “new” language (in the 60s)? – to build accurate models for the description and

simulation of complex man-machine systems.

The modularization occurs at the physical object

level (not at a procedural level)

Page 5: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

5

The Beginnings

Simula 67 was designed for system simulation (in Norway by Kristen Nygaard and Ole-Johan Dahl)

– Caller and called subprogram had equal relationship

– First notion of objects including class/instance distinctions

– Ability to put code inside an instance to be executed

– The class concept was first used here

Kristen Nygaard invented inheritance

– Kristen Won the Turing award for 2002

Page 6: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

6

One way to Start OOA and D

Identify candidate objects that model

(shape) the system as a natural and

sensible set of abstractions

Determine main responsibility of each

– what an instance of the class must be able to

do and what is should remember

• This is part of Responsibility Driven Design ala

Rebecca Wirfs-Brocks

Page 7: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

7

System Specification

The college library has requested a system that supports a small set of library operations. The librarian allows a student to borrow certain items, return those borrowed items, and pay fees. Late fees and due dates have been established at the following rates:

Late fee Length of Borrow

Books $0.50 per day 14 days

Video tapes $5.00 plus 1.50 each additional day 2 days

CDs $2.50 per day 7 days

The due date is set when the borrowed item is checked out. A student with three (3) borrowed items, one late item, or late fees greater than $25.00 may not borrow anything new.

Page 8: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

8

Identify candidate objects

Candidate objects that model a solution with main responsibility. The model (no GUIs, events, networking)

– Librarian: Coordinates activities

– Student, renamed Borrower

– Book: Knows due date, late fees, Borrower, checkin...

– Video: Knows due date, late fees, Borrower, checkin...

– CD: Know due date, late fees, Borrower, checkin...

– Three borrowed books: A collection of the things that canbe borrowed, name it LendableList

– BorrowList: maintains all possible borrowers

Page 9: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

9

A Sketch

Page 10: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

10

What do Books, Videos, and CDs

have in common?

Common responsibilities (methods and data):

– know call number, borrower, availability

– can be borrowed

– can be returned

Differences:

– compute due date

– compute late fee

– may have additional state

• Books have an author, CDs an artist

Page 11: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

11

When is inheritance appropriate?

Object-Oriented Design Heuristic:If two or more classes have common data

and behavior, then those classes should

inherit from a common base class that

captures those data and methods

Page 12: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

12

An inheritance hierarchy

Lendable

Book CDRom Video

The abstract class never instantiated

Lendable is also known as the base class or superclass

Lendable is shown to abstract (in italic)

Book, CD, and Video are concrete subclasses

Page 13: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

13

Why not have just one class?

Some of the behavior differs

– Determine due date (2, 7, or 14 days)

– Compute late fee not always daysLate * dayLateFee

Data differs

– books have ISBNs and videos may have studio name

Inheritance

– allows you to share implementations

– allows one change in a common method to affect all

– allows other Lendables to be added later more easily

Page 14: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

14

Examples of inheritance in Java

You've seen HAS-A relationships:

– A Song HAS-A most recent date played

Inheritance models IS-A relationships:

– an oval IS-A shape

– a rectangle IS-A shape

– MyFrame extends JFrame makes MyFrame a

JFrame with additional methods and listeners

for a specific application

Page 15: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

15

Java Examples of Inheritance

All Exceptions extend Exception:

– RunTimeException IS-AN Exception

– NullPointerException IS-A RunTimeException

Many classes extend the Component class

– a JButton IS-A Component

– a JTextField IS-A Component

A GregorianCalender IS-A Calendar

Vector and ArrayList extends AbstractList

NoSongInQueueException IS-A RuntimeException

Page 16: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

16

Designing An Inheritance Hierarchy

Start with an abstract class to define common-alities and differences (abstract methods)

public abstract class Lendable {

private instance variables

constructor(s)

methods that Lendable implements when the behavior is common to all of its subclasses (what is common)

abstract methods the subclasses must implement thatto represent what varies

}

Page 17: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

17

Some common data fields

Every class in the hierarchy ended with these

private instance variables in class Lendable:

– Subclasses can not directly reference these

private String callNumber;

private String title;

private boolean availability;

private String borrowerID;

private DayCounter dueDate;

Page 18: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

18

Lendable's constructor

Constructor needs a callNumber and title since it

seems that all Lendables will need both

The actual values will later come from the subclasses

constructor

public Lendable(String callNumber, String initTitle) {

callNumber = callNumber; // from subclass

title = initTitle; // from subclass

// Initialize others in a special way

borrowerID = null;

dueDate = null;

availability = true;

}

Page 19: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

19

Common Behavior

public String getCallNumber() {

return callNumber;

}

public String getTitle() {

return title;

}

public boolean isAvailable() {

return availability;

}

public DayCounter getDueDate() {

return dueDate;

}

Page 20: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

20

Common Behavior continued

public int daysLate() {

// return a positive if dueDate is before today

DayCounter today = new DayCounter();

return dueDate.daysFrom(today);

}

public boolean isOverdue() {

if(this.isAvailable())

return false; // not even checked out

// Or check to see if this Lendable is overdue

DayCounter today = new DayCounter();

// Return true if today is greater than

// the due date for this Lendable

return daysLate() > 0;

}

Page 21: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

21

Common Behavior continued

public boolean checkSelfIn() {

if(this.isAvailable())

return false;

else { // Adjust state so this is checked out

dueDate = null;

availability = true;

return true;

}

}

Page 22: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

22

checkSelfOut split between Lendable

and its subclasses

// called from a subclass checkSelfOut

protected

void checkOutAnyLendable(String borrowerID,

int borrowLength) {

// Record who is borrowing this Lendable

this.borrowerID = borrowerID;

// Set the due date

dueDate = new DayCounter(); // today's date

dueDate.adjustDaysBy(borrowLength);

// Mark this as no longer available

availability = false;

}

Page 23: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

23

Protected

The protected access mode means that subclasses inherit this method (inherit all public & protected elements)

It's invoked by the subclass's checkSelfOutmethod.

Page 24: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

24

Design Issues

So far, good design for the following reasons:

– subclasses can't change the private variables of the superclass, even though the subclass has them

– doesn't require a bunch of setter methods in Lendable for subclasses to modify their own instance variables

– the common behavior is in the superclass

• the same thing is done for all subclasses

– We'll get a polymorphic checkSelfOut message

Page 25: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

25

Abstract methods

– Subclasses differ: setDueDate getLateFee • declare the appropriate methods abstract, to force sub-classes to

implement them in their own appropriate ways

public abstract class Lendable {

// Don't really borrow a Lendable, // but you borrow a book

// Nor do you or eat a Fruit// but you can Eat a Peach

// Subclass must implement these two methods

abstract public void checkSelfOut(String ID);

abstract public double getLateFee();

} // Done with Lendable for now

Page 26: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

26

What can a subclass do?

General form for inheriting a Java class:public class subclass extends superclass {

// subclass inherits all public and

// protected methods of superclass

may add class constants

may add instance variables

may add 1 to many constructors

may add new public and protected methods

may override methods in the superclass

may add additional private methods

}

Page 27: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

27

The Constructor and Super

A subclass typically defines its constructor. If not

– You will still get a default constructor with 0 parameters that automatically calls the base class constructor

Constructors in a subclass often call the superclass constructor to initialize the objects

Access superclass with the keyword super

– can pass along arguments

super(callNum, title)

– if used, super must be the first message in the constructor of the derived classes

Page 28: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

28

Book extends Lendable

public class Book extends Lendable {

public static final int DAYS_TO_BORROW_BOOK = 14;

public static final double BOOK_LATE_DAY_FEE = 0.50;

private String author;

public Book(String callNumber,

String title,

String author){

super(callNumber, title);

author = author;

}

public String getAuthor() {

return author;

}

}

Page 29: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

29

Complete both required methods

checkSelfOut delegates some work to

Lendable and passes along the unique

information Book.DAYS_TO_BORROW_BOOK/** Modify the state of this object so it is borrowed

* @param borrowerID: The identification of borrower

*/

@Override

public void checkSelfOut(String borrowerID) {

checkOutAnyLendable(borrowerID,

Book.DAYS_TO_BORROW_BOOK);

}

Page 30: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

30

getLateFee differs among the

Lendable subclasses@Override

public double getLateFee() {

if(this.isAvailable()) // Not even checked out!

return 0.00;

else {

// A positive daysLate means due date has passed

int daysOverdue = this.daysLate();

if(daysOverdue > 0) // This Lendable is overdue

return daysOverdue * Book.BOOK_LATE_DAY_FEE;

else

return 0.00; // The due date has not passed

}

}

}

Page 31: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

31

A few assertions@Test

public void testGetters() {

// Show that Book has many methods via inheritance

Book aBook = new Book("QA76.1", "C++", "Jo");

assertTrue(aBook.isAvailable());

assertFalse(aBook.isOverdue());

assertNull(aBook.getBorrowerID());

assertEquals("QA76.1", aBook.getCallNumber());

assertEquals("C++", aBook.getTitle());

assertEquals("Jo", aBook.getAuthor());

// Use the 2 methods that once were abstract and

// now have concrete realizations

assertEquals(0.00, aBook.getLateFee(), 1e-12);

assertTrue(aBook.isAvailable());

aBook.checkSelfOut("Rick");

assertFalse(aBook.isAvailable());

}

Page 32: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

32

Adding Other Subclasses

Extend Lendable

Optional: add class constants

– days to borrow, late fee amounts

Add a constructor that passes along arguments to the constructor in Lendable (super)

Add the methods that Lendable requires of all sublclasses: use Override

– checkSelfOut

– getLateFee

Page 33: OO Design with Inheritance - University of Arizona · 2010. 9. 23. · OO Design with Inheritance C Sc 335 Rick Mercer. 2 ... reason to use inheritance –Show the objects found for

33