HFOOAD Chapter 5 Interlude

Preview:

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

Citation preview

HFOOAD Chapter 5 Interlude

OO Catastrophe!

2

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

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

5

6

Isn’t encapsulation just about hiding implementation?

ENCAPSULATION

Solution 1

7

Solution 2

8

9

10

11

Manage changeManage changeManage change…

Final Catastrophe challenge

12

Our solution

13

Recommended