inheritance examples

21
Inheritance Examples 1

Upload: toki

Post on 24-Feb-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Inheritance Examples. Real-world Examples: Animals. Source code: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming. Animal. Dog. Cat. Example #2: Timepieces. Multiple inheritance. A derived class can have more than one base class - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Inheritance Examples

1

Inheritance Examples

Page 2: Inheritance Examples

2

Real-world Examples: Animals

Source code: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Animal

Dog Cat

Page 3: Inheritance Examples

3

Example #2: Timepieces

Page 4: Inheritance Examples

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

5

Example #3: Shapes

Basic Shape

Circle Polygon Ellipse

Square HexagonRectangle

Page 6: Inheritance Examples

6

Example #4: Wireless TelephonyMobile initiated

message

Registration Call Processing SMS

Origination Page Response

Page 7: Inheritance Examples

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

8

Inheritance Examples in Java and C++

Page 9: Inheritance Examples

9

Back to Example #1: Animals

Animal

Dog Cat

Want to group them together &make them talk?

Page 10: Inheritance Examples

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

11

Add Human?

Animal

Dog Cat

Human

Want to group them together &make them talk?

Page 12: Inheritance Examples

12

C++

Animal

Dog Cat

Human

AnimalActions

Page 13: Inheritance Examples

13

Java: Interface

Animal

Dog Cat

Human

AnimalActions

Page 14: Inheritance Examples

14

Example from Wiki:Bad use of Inheritance

Parrot Human

Whistler

Page 15: Inheritance Examples

15

Example from Wiki

WhistlerParrot Human

Page 16: Inheritance Examples

16

Concepts

• Interface

Page 17: Inheritance Examples

17

C++

Animal

Dog Cat Human

AnimalActions

Page 18: Inheritance Examples

18

Java: Interface

Animal

Dog CatHuman

AnimalActions

Page 19: Inheritance Examples

19

Inheritance Examples in Java and C++

Page 20: Inheritance Examples

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

21