hfooad chapter 5 interlude

13
HFOOAD Chapter 5 Interlude OO Catastrophe!

Upload: kelly-baird

Post on 02-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

HFOOAD Chapter 5 Interlude. OO Catastrophe!. Two possible answers. Interface. Abstract class. Defines behavior Can have implementation code Cannot be instantiated A class can inherit from a single abstract class Unless the language supports multiple inheritance. Defines behavior - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: HFOOAD Chapter 5 Interlude

HFOOAD Chapter 5 Interlude

OO Catastrophe!

Page 2: HFOOAD Chapter 5 Interlude

2

Page 3: HFOOAD Chapter 5 Interlude

Interface

‣ Defines behavior

‣ Contract

‣ Cannot be instantiated

‣ A class can implement multiple interfaces

‣ In languages that support interfaces

Abstract class

‣ Defines behavior

‣ Can have implementation code

‣ Cannot be instantiated

‣ A class can inherit from a single abstract class

‣ Unless the language supports multiple inheritance

3

Two possible answers

Page 4: HFOOAD Chapter 5 Interlude

4

How do I know when to use an interface and when to use an abstract class?

If (almost) all classes implementing the behavior would have the same code, then you can use an abstract class to implement it.

Avoid repeating code with abstract classes

Program to interfaces

Page 5: HFOOAD Chapter 5 Interlude

5

Page 6: HFOOAD Chapter 5 Interlude

6

Isn’t encapsulation just about hiding implementation?

ENCAPSULATION

Page 7: HFOOAD Chapter 5 Interlude

Solution 1

7

Page 8: HFOOAD Chapter 5 Interlude

Solution 2

8

Page 9: HFOOAD Chapter 5 Interlude

9

Page 10: HFOOAD Chapter 5 Interlude

10

Page 11: HFOOAD Chapter 5 Interlude

11

Manage changeManage changeManage change…

Page 12: HFOOAD Chapter 5 Interlude

Final Catastrophe challenge

12

Page 13: HFOOAD Chapter 5 Interlude

Our solution

13