Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Inheritance Examples
description
Transcript of Inheritance Examples
![Page 1: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/1.jpg)
1
Inheritance Examples
![Page 2: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/2.jpg)
2
Real-world Examples: Animals
Source code: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
Animal
Dog Cat
![Page 3: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/3.jpg)
3
Example #2: Timepieces
![Page 4: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/4.jpg)
4
Multiple inheritance– A derived class can have more than one base class– Java does not support it – uses “interface” instead.
![Page 5: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/5.jpg)
5
Example #3: Shapes
Basic Shape
Circle Polygon Ellipse
Square HexagonRectangle
![Page 6: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/6.jpg)
6
Example #4: Wireless TelephonyMobile initiated
message
Registration Call Processing SMS
Origination Page Response
![Page 7: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/7.jpg)
7
Value of inheritance?
• Helps us to break software into manageable pieces
• Existing classes can be morphed to design new classes – code reuse
• Enables us to group different types of objects together and do some action on all of them.
![Page 8: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/8.jpg)
8
Inheritance Examples in Java and C++
![Page 9: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/9.jpg)
9
Back to Example #1: Animals
Animal
Dog Cat
Want to group them together &make them talk?
![Page 10: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/10.jpg)
10
Concepts
• Static binding vs. dynamic binding• Polymorphism using virtual methods• Abstract base class• Ability of base class pointers to point to
derived class objects
![Page 11: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/11.jpg)
11
Add Human?
Animal
Dog Cat
Human
Want to group them together &make them talk?
![Page 12: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/12.jpg)
12
C++
Animal
Dog Cat
Human
AnimalActions
![Page 13: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/13.jpg)
13
Java: Interface
Animal
Dog Cat
Human
AnimalActions
![Page 14: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/14.jpg)
14
Example from Wiki:Bad use of Inheritance
Parrot Human
Whistler
![Page 15: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/15.jpg)
15
Example from Wiki
WhistlerParrot Human
![Page 16: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/16.jpg)
16
Concepts
• Interface
![Page 17: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/17.jpg)
17
C++
Animal
Dog Cat Human
AnimalActions
![Page 18: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/18.jpg)
18
Java: Interface
Animal
Dog CatHuman
AnimalActions
![Page 19: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/19.jpg)
19
Inheritance Examples in Java and C++
![Page 20: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/20.jpg)
20
References• http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming -
Animals : Inheritance coding examples in Java/C++/…• http://en.wikipedia.org/wiki/Interface_%28Java%29 – Java Interfaces
![Page 21: Inheritance Examples](https://reader036.fdocuments.us/reader036/viewer/2022062323/56816547550346895dd7c00c/html5/thumbnails/21.jpg)
21