Chapter 10: PowerPoint

Download Report

Transcript Chapter 10: PowerPoint

Chapter 10
Inheritance
STARTING OUT WITH
Python
First Edition
by
Tony Gaddis
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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.
1-2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-2
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
1-3
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-3
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
1-4
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-4
10.1 Introduction to Inheritance
Inheritance and the
“Is a” Relationship
Program 10-1 (Lines 1
through 44 of vehicles.py)
1-5
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-5
10.1 Introduction to Inheritance
Inheritance and the “Is a” Relationship
Program 10-2 (Lines 45
through 72 of vehicles.py)
1-6
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-6
10.1 Introduction to Inheritance
Inheritance and the “Is a” Relationship
Program 10-3
(car_demo.py)
1-7
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-7
10.1 Introduction to Inheritance
Inheritance in UML Diagrams
Figure 10-2 UML diagram
showing inheritance
1-8
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-8
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.
1-9
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-9
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.
1-10
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-10
10.2 Polymorphism
Program 10-10
(polymorphism_demo.py)
1-11
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-11
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)
1-12
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-12
10.2 Polymorphism
The isinstance Function
Program 10-12
(polymorphism_demo2.py)
1-13
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
10-13
Chapter 10
Inheritance
QUESTIONS
?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley