Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Inheritance Examples
1
Real-world Examples: Animals
Source code: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
Animal
Dog Cat
2
3Example #2: Timepieces
Multiple inheritance
– A derived class can have more than one base class
– Java does not support it – uses “interface” instead.
4
Example #3: Shapes 
Basic Shape
Circle Polygon Ellipse
Square HexagonRectangle
5
Example #4: Wireless Telephony
Mobile initiated 
message
Registration Call Processing SMS
Origination Page Response
6
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.
7
Inheritance Examples in 
Java and C++
8
Back to Example #1: 
Animals
9
Animal
Dog Cat
Want to group them together &
make them talk?

Concepts
• Static binding vs. dynamic binding
• Polymorphism using virtual methods
• Abstract base class
• Ability of base class pointers to point to 
derived class objects
10
Add Human?
11
Animal
Dog Cat
Human
Want to group them together &
make them talk?
C++
12
Animal
Dog Cat
Human
AnimalActions
Java: Interface
13
Animal
Dog Cat
Human
AnimalActions
Example from Wiki:
Bad use of Inheritance
14
Parrot Human
Whistler
Example from Wiki
15
WhistlerParrot Human
Concepts
• Interface
16
C++
17
Animal
Dog Cat Human
AnimalActions
Java: Interface
18
Animal
Dog Cat
Human
AnimalActions
Inheritance Examples in 
Java and C++
19
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
20