1 / 14

STARTING OUT WITH Python First Edition by Tony Gaddis

Chapter 10 Inheritance. STARTING OUT WITH Python First Edition by Tony Gaddis. 10.1 Introduction to Inheritance. Concept: Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. 10.1 Introduction to Inheritance.

mdipietro
Download Presentation

STARTING OUT WITH Python First Edition by Tony Gaddis

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. Chapter 10Inheritance STARTING OUT WITH PythonFirst Edition by Tony Gaddis

  2. 10.1 Introduction to Inheritance Concept: Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends.

  3. 10.1 Introduction to Inheritance • Inheritance and the “Is a” Relationship • An “is a” relationship exists between objects. This means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. • A poodle is a dog • A car is a vehicle • A flower is a plant • A rectangle is a shape • A football player is an athlete

  4. 10.1 Introduction to Inheritance • Inheritance and the “Is a” Relationship • Superclass (base class)– general class • Subclass (derived class) – specialized class • Inherits attributes and methods from the superclass without any of them having to be rewritten

  5. 10.1 Introduction to Inheritance • Inheritance and the “Is a” Relationship Program 10-1 (Lines 1 through 44 of vehicles.py)

  6. 10.1 Introduction to Inheritance • Inheritance and the “Is a” Relationship Program 10-2 (Lines 45 through 72 of vehicles.py)

  7. 10.1 Introduction to Inheritance • Inheritance and the “Is a” Relationship Program 10-3 (car_demo.py)

  8. 10.1 Introduction to Inheritance • Inheritance in UML Diagrams Figure 10-2 UML diagram showing inheritance

  9. 10.2 Polymorphism Concept: Polymorphism allows subclasses to have methods with the same names as methods in their superclasses. It gives the ability for a program to call the correct method depending on the type of object that is used to call it.

  10. 10.2 Polymorphism • Polymorphism refers to an object’s ability to take different forms. • Behaviors: • The ability to define a method in a superclass, and then define a method with the same name in a subclass. When a subclass method has the same name as a superclass method, it is often said that the subclass method overrides the superclass method. • The ability to call the correct version of an overridden method, depending on the type of object that is used to call it. If a subclass object is used to call an overridden method, then the subclass’s version of the method is the one that will execute. If a superclass object is used to call an overridden method, then the superclass’s version of the method is the one that will execute.

  11. 10.2 Polymorphism Program 10-10 (polymorphism_demo.py)

  12. 10.2 Polymorphism • The isinstance Function • Is used to determine whether an object is an instance of a specific class or a subclass of that class • isinstance(object, ClassName)

  13. 10.2 Polymorphism • The isinstance Function Program 10-12 (polymorphism_demo2.py)

  14. Chapter 10Inheritance QUESTIONS ?

More Related