Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python First Edition STARTING OUT WITH Chapter 10 Inheritance

Similar presentations


Presentation on theme: "Python First Edition STARTING OUT WITH Chapter 10 Inheritance"— Presentation transcript:

1 Python First Edition STARTING OUT WITH Chapter 10 Inheritance
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 (Lines 1 through 44 of vehicles.py)

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

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

8 10.1 Introduction to Inheritance
Inheritance in UML Diagrams Figure 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 (polymorphism_demo.py)

12 isinstance(object, ClassName)
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 (polymorphism_demo2.py)

14 Chapter 10 Inheritance QUESTIONS ?


Download ppt "Python First Edition STARTING OUT WITH Chapter 10 Inheritance"

Similar presentations


Ads by Google