1 / 21

Inheritance Examples

This article explores the concept of inheritance in object-oriented programming (OOP) through real-world examples. It illustrates how classes can be derived from base classes, enabling code reuse and enhancing manageability in software design. Key examples include animal hierarchy (Animal, Dog, Cat) and geometric shapes (Circle, Polygon, Square, etc.). The article discusses single and multiple inheritance, emphasizing Java's approach with interfaces, and explains important concepts such as polymorphism, static and dynamic binding, and abstract classes.

toki
Télécharger la présentation

Inheritance Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Examples

  2. Real-world Examples: Animals Source code: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Animal Dog Cat

  3. Example #2: Timepieces

  4. Multiple inheritance • A derived class can have more than one base class • Java does not support it – uses “interface” instead.

  5. Example #3: Shapes Basic Shape Circle Polygon Ellipse Square Rectangle Hexagon

  6. Example #4: Wireless Telephony Mobile initiated message Registration Call Processing SMS Origination Page Response

  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.

  8. Inheritance Examples in Java and C++

  9. Back to Example #1: Animals Animal Dog Cat Want to group them together & make them talk?

  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

  11. Add Human? Animal Human Dog Cat Want to group them together & make them talk?

  12. C++ AnimalActions Animal Human Dog Cat

  13. Java: Interface AnimalActions Animal Human Dog Cat

  14. Example from Wiki:Bad use of Inheritance Whistler Parrot Human

  15. Example from Wiki Whistler Human Parrot

  16. Concepts • Interface

  17. C++ Animal AnimalActions Dog Cat Human

  18. Java: Interface AnimalActions Animal Human Dog Cat

  19. Inheritance Examples in Java and C++

  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

More Related