0% found this document useful (0 votes)
79 views

Inheritance3 PDF

The document provides examples of inheritance in object-oriented programming using animals. In example 1, the classes Dog and Cat inherit from the base class Animal, allowing them to be grouped together. Example 2 shows multiple inheritance with a class inheriting from more than one base class. Example 3 demonstrates inheritance with shapes like Circle and Square inheriting from the base class Shape. The document also discusses concepts like polymorphism, abstract base classes, and interfaces as an alternative to multiple inheritance in Java.

Uploaded by

akon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Inheritance3 PDF

The document provides examples of inheritance in object-oriented programming using animals. In example 1, the classes Dog and Cat inherit from the base class Animal, allowing them to be grouped together. Example 2 shows multiple inheritance with a class inheriting from more than one base class. Example 3 demonstrates inheritance with shapes like Circle and Square inheriting from the base class Shape. The document also discusses concepts like polymorphism, abstract base classes, and interfaces as an alternative to multiple inheritance in Java.

Uploaded by

akon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Inheritance Examples

1
Real-world Examples: Animals

Animal

Dog Cat

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

2
Example #2: Timepieces

3
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 Rectangle Hexagon

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

Animal

Dog Cat

Want to group them together &


make them talk?
9
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?

Animal Human

Dog Cat

Want to group them together &


make them talk?
11
C++

AnimalActions

Animal Human

Dog Cat

12
Java: Interface

AnimalActions
Animal

Human
Dog Cat

13
Example from Wiki:
Bad use of Inheritance

Whistler

Parrot Human

14
Example from Wiki

Parrot Whistler Human

15
Concepts
• Interface

16
C++

Animal AnimalActions

Dog Cat Human

17
Java: Interface

Animal AnimalActions

Human
Dog Cat

18
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

You might also like