Sei sulla pagina 1di 22

Inheritance Examples

Real-world Examples: Animals


Animal

$og

%at

Source code: http://en.wikipedia.org/wiki/Pol morphism!in!o"#ect-oriented!programming


&

Example (&: )imepieces

'

*ultiple inheritance
A deri+ed class can ha+e more than one "ase class ,a+a does not support it - uses .inter/ace0 instead.

Example (': Shapes


2asic Shape

%ircle

Pol gon

Ellipse

S3uare

Rectangle

4exagon

Example (1: 6ireless )elephon


*o"ile initiated message

Registration

%all Processing

S*S

7rigination

Page Response

9alue o/ inheritance:
4elps us to "reak so/tware into managea"le pieces Existing classes can "e morphed to design new classes - code reuse Ena"les us to group di//erent t pes o/ o"#ects together and do some action on all o/ them.

Inheritance Examples in ,a+a and %<<

2ack to Example (1: Animals


Animal

$og

%at

Want to group them together & make them talk?


>

%oncepts
Static "inding +s. d namic "inding Pol morphism using +irtual methods A"stract "ase class A"ilit o/ "ase class pointers to point to deri+ed class o"#ects

1?

Add 4uman:
Animal 4uman

$og

%at

Want to group them together & make them talk?


11

%<<
AnimalActions

Animal

4uman

$og

%at

1&

,a+a: Inter/ace

Animal

AnimalActions

4uman $og %at

1'

Example /rom 6iki: 2ad use o/ Inheritance

6histler

Parrot

4uman

11

Example /rom 6iki

Parrot

6histler

4uman

15

%oncepts
Inter/ace

18

%<<
Animal AnimalActions

$og

%at

4uman

1;

,a+a: Inter/ace

Animal

AnimalActions

4uman $og %at

1=

Inheritance Examples in ,a+a and %<<

1>

Re/erences
http://en.wikipedia.org/wiki/Pol morphism!in!o"#ect-oriented!programming Animals : Inheritance coding examples in ,a+a/%<</@ http://en.wikipedia.org/wiki/Inter/ace!A&=,a+aA&> - ,a+a Inter/aces

&?

Potrebbero piacerti anche