SlideShare uma empresa Scribd logo
1 de 14
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
C HA PT E R 11
Inheritance
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Topics
Introduction to Inheritance
Polymorphism
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to Inheritance
In the real world, many objects are a
specialized version of more general
objects
Example: grasshoppers and bees are
specialized types of insect
In addition to the general insect characteristics,
they have unique characteristics:
Grasshoppers can jump
Bees can sting, make honey, and build hives
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to Inheritance
(cont’d.)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Inheritance and the “Is a”
Relationship
“Is a” relationship: exists when one
object is a specialized version of
another object
Specialized object has all the characteristics
of the general object plus unique
characteristics
Example: Rectangle is a shape
Daisy is a flower
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Inheritance and the “Is a”
Relationship (cont’d.)
Inheritance: used to create an “is a”
relationship between classes
Superclass (base class): a general
class
Subclass (derived class): a specialized
class
An extended version of the superclass
Inherits attributes and methods of the superclass
New attributes and methods can be added
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Inheritance and the “Is a”
Relationship (cont’d.)
For example, need to create classes for
cars, pickup trucks, and SUVs
All are automobiles
Have a make, year model, mileage, and price
This can be the attributes for the base class
In addition:
Car has a number of doors
Pickup truck has a drive type
SUV has a passenger capacity
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Inheritance and the “Is a”
Relationship (cont’d.)
In a class definition for a subclass:
To indicate inheritance, the superclass name
is placed in parentheses after subclass name
Example: class Car(Automobile):
The initializer method of a subclass calls the
initializer method of the superclass and then
initializes the unique data attributes
Add method definitions for unique methods
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Inheritance in UML Diagrams
• In UML diagram, show inheritance by
drawing a line with an open arrowhead
from subclass to superclass
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Polymorphism
Polymorphism: an object’s ability to
take different forms
Essential ingredients of polymorphic
behavior:
Ability to define a method in a superclass and
override it in a subclass
Subclass defines method with the same name
Ability to call the correct version of overridden
method depending on the type of object that
called for it
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Polymorphism (cont’d.)
In previous inheritance examples
showed how to override the __init__
method
Called superclass __init__ method and
then added onto that
The same can be done for any other
method
The method can call the superclass
equivalent and add to it, or do something
completely different
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The isinstance Function
Polymorphism provides great flexibility
when designing programs
AttributeError exception: raised
when a method is receives an object
which is not an instance of the right
class
isinstance function: determines
whether object is an instance of a class
Format: isinstance(object, class)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary
This chapter covered:
Inheritance, including:
“Is a” relationships
Subclasses and superclasses
Defining subclasses and initializer methods
Depicting inheritance in UML diagrams
Polymorphism
The isinstance function

Mais conteúdo relacionado

Semelhante a Inheritance

Semelhante a Inheritance (7)

Object Oriented Relationships
Object Oriented RelationshipsObject Oriented Relationships
Object Oriented Relationships
 
Data modeling
Data modelingData modeling
Data modeling
 
Introduction to Classes and Objects
Introduction to Classes and ObjectsIntroduction to Classes and Objects
Introduction to Classes and Objects
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Inheritance and its necessity in java.ppt
Inheritance and its necessity in java.pptInheritance and its necessity in java.ppt
Inheritance and its necessity in java.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
07. Class Diagram.ppt
07. Class Diagram.ppt07. Class Diagram.ppt
07. Class Diagram.ppt
 

Mais de Nicole Ryan

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving PerformanceNicole Ryan
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search enginesNicole Ryan
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object modelNicole Ryan
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and AudioNicole Ryan
 
Working with Images
Working with ImagesWorking with Images
Working with ImagesNicole Ryan
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and AnimationNicole Ryan
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web FormsNicole Ryan
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and TablesNicole Ryan
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your websiteNicole Ryan
 
Working with Links
Working with LinksWorking with Links
Working with LinksNicole Ryan
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSSNicole Ryan
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSSNicole Ryan
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSSNicole Ryan
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web ContentNicole Ryan
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your WebsiteNicole Ryan
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationNicole Ryan
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 
Programming Logic and Design: Arrays
Programming Logic and Design: ArraysProgramming Logic and Design: Arrays
Programming Logic and Design: ArraysNicole Ryan
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with DataNicole Ryan
 

Mais de Nicole Ryan (20)

Testing and Improving Performance
Testing and Improving PerformanceTesting and Improving Performance
Testing and Improving Performance
 
Optimizing a website for search engines
Optimizing a website for search enginesOptimizing a website for search engines
Optimizing a website for search engines
 
Javascript programming using the document object model
Javascript programming using the document object modelJavascript programming using the document object model
Javascript programming using the document object model
 
Working with Video and Audio
Working with Video and AudioWorking with Video and Audio
Working with Video and Audio
 
Working with Images
Working with ImagesWorking with Images
Working with Images
 
Creating Visual Effects and Animation
Creating Visual Effects and AnimationCreating Visual Effects and Animation
Creating Visual Effects and Animation
 
Creating and Processing Web Forms
Creating and Processing Web FormsCreating and Processing Web Forms
Creating and Processing Web Forms
 
Organizing Content with Lists and Tables
Organizing Content with Lists and TablesOrganizing Content with Lists and Tables
Organizing Content with Lists and Tables
 
Social media and your website
Social media and your websiteSocial media and your website
Social media and your website
 
Working with Links
Working with LinksWorking with Links
Working with Links
 
Formatting text with CSS
Formatting text with CSSFormatting text with CSS
Formatting text with CSS
 
Laying Out Elements with CSS
Laying Out Elements with CSSLaying Out Elements with CSS
Laying Out Elements with CSS
 
Getting Started with CSS
Getting Started with CSSGetting Started with CSS
Getting Started with CSS
 
Structure Web Content
Structure Web ContentStructure Web Content
Structure Web Content
 
Getting Started with your Website
Getting Started with your WebsiteGetting Started with your Website
Getting Started with your Website
 
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and AnimationChapter 12 Lecture: GUI Programming, Multithreading, and Animation
Chapter 12 Lecture: GUI Programming, Multithreading, and Animation
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 
Programming Logic and Design: Arrays
Programming Logic and Design: ArraysProgramming Logic and Design: Arrays
Programming Logic and Design: Arrays
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with Data
 

Último

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Último (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

Inheritance

  • 1. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C HA PT E R 11 Inheritance
  • 2. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics Introduction to Inheritance Polymorphism
  • 3. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Inheritance In the real world, many objects are a specialized version of more general objects Example: grasshoppers and bees are specialized types of insect In addition to the general insect characteristics, they have unique characteristics: Grasshoppers can jump Bees can sting, make honey, and build hives
  • 4. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Inheritance (cont’d.)
  • 5. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance and the “Is a” Relationship “Is a” relationship: exists when one object is a specialized version of another object Specialized object has all the characteristics of the general object plus unique characteristics Example: Rectangle is a shape Daisy is a flower
  • 6. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance and the “Is a” Relationship (cont’d.) Inheritance: used to create an “is a” relationship between classes Superclass (base class): a general class Subclass (derived class): a specialized class An extended version of the superclass Inherits attributes and methods of the superclass New attributes and methods can be added
  • 7. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance and the “Is a” Relationship (cont’d.) For example, need to create classes for cars, pickup trucks, and SUVs All are automobiles Have a make, year model, mileage, and price This can be the attributes for the base class In addition: Car has a number of doors Pickup truck has a drive type SUV has a passenger capacity
  • 8. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance and the “Is a” Relationship (cont’d.) In a class definition for a subclass: To indicate inheritance, the superclass name is placed in parentheses after subclass name Example: class Car(Automobile): The initializer method of a subclass calls the initializer method of the superclass and then initializes the unique data attributes Add method definitions for unique methods
  • 9. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance in UML Diagrams • In UML diagram, show inheritance by drawing a line with an open arrowhead from subclass to superclass
  • 10. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
  • 11. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polymorphism Polymorphism: an object’s ability to take different forms Essential ingredients of polymorphic behavior: Ability to define a method in a superclass and override it in a subclass Subclass defines method with the same name Ability to call the correct version of overridden method depending on the type of object that called for it
  • 12. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polymorphism (cont’d.) In previous inheritance examples showed how to override the __init__ method Called superclass __init__ method and then added onto that The same can be done for any other method The method can call the superclass equivalent and add to it, or do something completely different
  • 13. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The isinstance Function Polymorphism provides great flexibility when designing programs AttributeError exception: raised when a method is receives an object which is not an instance of the right class isinstance function: determines whether object is an instance of a class Format: isinstance(object, class)
  • 14. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary This chapter covered: Inheritance, including: “Is a” relationships Subclasses and superclasses Defining subclasses and initializer methods Depicting inheritance in UML diagrams Polymorphism The isinstance function