hfooad chapter 5 interlude
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 PresentationTRANSCRIPT
![Page 1: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/1.jpg)
HFOOAD Chapter 5 Interlude
OO Catastrophe!
![Page 2: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/2.jpg)
2
![Page 3: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/5.jpg)
5
![Page 6: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/6.jpg)
6
Isn’t encapsulation just about hiding implementation?
ENCAPSULATION
![Page 7: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/7.jpg)
Solution 1
7
![Page 8: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/8.jpg)
Solution 2
8
![Page 9: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/9.jpg)
9
![Page 10: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/10.jpg)
10
![Page 11: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/11.jpg)
11
Manage changeManage changeManage change…
![Page 12: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/12.jpg)
Final Catastrophe challenge
12
![Page 13: HFOOAD Chapter 5 Interlude](https://reader035.vdocuments.site/reader035/viewer/2022062802/56814583550346895db261ff/html5/thumbnails/13.jpg)
Our solution
13