22
Inheritance Examples 1

Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

  • Upload
    haliem

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Inheritance Examples

1

Page 2: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Real-world Examples: Animals

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

Animal

Dog Cat

2

Page 3: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

3

Example #2: Timepieces

Page 4: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Multiple inheritance

– A derived class can have more than one base class

– Java does not support it – uses “interface” instead.

4

Page 5: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Example #3: Shapes

Basic Shape

Circle Polygon Ellipse

Square HexagonRectangle

5

Page 6: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Example #4: Wireless Telephony

Mobile initiated

message

Registration Call Processing SMS

Origination Page Response

6

Page 7: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

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

Page 8: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Inheritance Examples in

Java and C++

8

Page 9: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Back to Example #1:

Animals

9

Animal

Dog Cat

Want to group them together &make them talk?

Page 10: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not
Page 11: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

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

Page 12: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Add Human?

11

Animal

Dog Cat

Human

Want to group them together &make them talk?

Page 13: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

C++

12

Animal

Dog Cat

Human

AnimalActions

Page 14: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Java: Interface

13

Animal

Dog Cat

Human

AnimalActions

Page 15: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Example from Wiki:

Bad use of Inheritance

14

Parrot Human

Whistler

Page 16: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Example from Wiki

15

WhistlerParrot Human

Page 17: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Concepts

• Interface

16

Page 18: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

C++

17

Animal

Dog Cat Human

AnimalActions

Page 19: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Java: Interface

18

Animal

Dog Cat

Human

AnimalActions

Page 20: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

Inheritance Examples in

Java and C++

19

Page 21: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not

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

Page 22: Inheritance Examples - The University of Texas at Dallasjeyv/AdvJava/inheritance3.pdf · Multiple inheritance – A derived class can have more than one base class – Java does not