Lecture 15: Inheritance



Similar documents
61A Lecture 16. Friday, October 11

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

Python for Rookies. Example Examination Paper

Chapter 13 - Inheritance

What are the tuition costs? The Tuition for the Prime of Life Yoga course is $ 975.

D06 PROGRAMMING with JAVA

CSC 314: Operating Systems Spring 2005

The Bellevue Center for Obesity & Weight Management. Program Director: Manish Parikh, MD WEIGHT LOSS SURGERY INFORMATION SEMINAR

WEIGHT LOSS SURGERY INFORMATION SEMINAR

BIO 315 Human Genetics - Online

How To Pass A Customer Service Course At Tstc

ICOM 4015: Advanced Programming

COURSE INFORMATION. 3. You learn the course policies and follow them.

T TH 7:30am to 8:30am. Exam on Thursday. T TH 1:30pm to 2:20pm. MWF 8:30am to 9:30am. T TH 11:30am to 12:30pm

USC VITERBI SCHOOL OF ENGINEERING INFORMATICS PROGRAM

Building Java Programs

Foundations of Programming

5. Advanced Object-Oriented Programming Language-Oriented Programming

Saving And Investing

Course Contract (Syllabus)* Financial Auditing-Accounting 58-62Z Fall 2012 (Catalyst* and Connect*)

**SYLLABUS IS SUBJECT TO CHANGE**

CS193j, Stanford Handout #10 OOP 3

ACCESS Nursing Programs Session 1 Center Valley Campus Only 8 Weeks Academic Calendar 8 Weeks

JOHN A. LOGAN COLLEGE L. Hines SM 14. ACC 100-VI BUSINESS ACCOUNTING 3 cr. (3-0) (online)

ACCESS Nursing Programs Session 1 Center Valley Campus Only 8 Weeks Academic Calendar 8 Weeks

Statistical Methods Online Course Syllabus

INFO/CS 4302 Web Information Systems. FT 2012 Week 1: Course Introduction

Precalculus Algebra Online Course Syllabus

Math 103, College Algebra Fall 2015 Syllabus TTh PM Classes

Paternity Testing. Chapter 23

CMSC Fundamentals of Computer Programming II (C++)

Introduction: How does a student get started? How much time does this course require per week?

Scope of this Course. Database System Environment. CSC 440 Database Management Systems Section 1

Course title: ART1300: Online Art Appreciation for Non-Art Majors

Academic Calendar - UNDERGRADUATE. All Other Classes Begin; Late Registration Begins. Last Day to Drop a Class without a W on Transcript;

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

JOHN A. LOGAN COLLEGE S. Trammell SM 13. BIO 225 GENETICS 3 cr. (3-0) (Online)

MYPY: A PYTHON VARIANT WITH SEAMLESS DYNAMIC AND STATIC TYPING. Jukka Lehtosalo University of Cambridge Computer Laboratory

Math 103, College Algebra Spring 2016 Syllabus MWF Day Classes MWTh Day Classes

8:00 a.m. Breakfast Meeting 11:00 a.m. Dentist 12:00 p.m. Lunch with Tom 1:00 p.m. Conversation with Head of UBS Research

Academic Calendar for Faculty

Earth Science 101 Introduction to Weather Fall 2015 Online

Course Contract (Syllabus)* Cost Accounting-Accounting 66-62Z Fall 2012 (Catalyst* and Connect*)

Course pack Accounting 202 Appendix C (back of textbook, after Chapter 14) Time Value of Money $$$$$$$$$$$$$$

How Do You Manage Your Time?

LOGOM 3300: Business Statistics Fall 2015

Homework 1. Comp 140 Fall 2008

Lesson 3 Save and Invest: The Power of Interest

ISM 4113: SYSTEMS ANALYSIS & DESIGN

CS 1361-D10: Computer Science I

Intermediate Economics II: Macroeconomics Economics 512: ONLINE Spring 2015

Course Syllabus Leadership Theory: Section 2 TTH Shaffer Hall Rm. 202 Spring Semester 2012

Physics 21-Bio: University Physics I with Biological Applications Syllabus for Spring 2012

School of Business ACCT2105/BUSI0027 (Subclasses A, B, C) Introduction to Management Accounting/ Management Accounting I Course Syllabus

Managerial Accounting

MGMT 361 (Hybrid) Human Resource Management

FINC 298 DEK Personal Financial Planning

CRIJ 1301 Introduction to Criminal Justice Course Syllabus: Summer 2015

IDH 1110 and 1112 Course Syllabus Interdisciplinary Studies in General Education I CRN West Campus MW 8:30-11:15am

GENERAL PSYCHOLOGY, PSC 1, Section 2

SJSU School of Journalism and Mass Communications Journalism 132, Section 1 Information Gathering Spring 2015

Cash Flow Analysis of Real Estate Projects: Inside and Outside an IRA And Pros & Cons of Investing Your IRA in an LLC

PSY 262 CHILD PSYCHOLOGY 3 cr. (3-0)

ACC 7145: ACCOUNTING SYSTEMS DESIGN AND CONTROL SYLLABUS FOR SUMMER SESSION 2014

Lecture: Mon 13:30 14:50 Fri 9:00-10:20 ( LTH, Lift 27-28) Lab: Fri 12:00-12:50 (Rm. 4116)

Lab 2: Swat ATM (Machine (Machine))

Syllabus: PHA 5271 Health Care Risk Management Spring

Psychology Mind and Society Mondays & Wednesdays, 2:00 3:50 pm, 129 McKenzie Hall Fall 2013 (CRN # 16067)

Upon completion of COMM 7, Interpersonal Communication, students will be able to:

Earth Science 102 Introduction to Physical Geology Fall 2015 Online

Payment-in-full due for Fall 2012 (ALL Students) LATE REGISTRATION for all students. Consortium Registration

TIME MANAGEMENT By Ann Marie Ross Created for CLASS and ECLP Revised Fall 2005 by Peggy Ozaki

Syllabus : CHM 234, General Organic Chemistry II : Spring 2015 Online/Hybrid Class SLN 15207

CS 100 Introduction to Computer Science Lab 1 - Playing with Python Due September 21/23, 11:55 PM

Engineering Problem Solving and Programming (CS 1133)

Your Family Tree. Afamily tree is a wonderful way to introduce. We re 4.1 LESSON. How we re related

FAST TRACK SUCCESS SYSTEM

Major Topics Covered in the Course

ACTG 051B: Intermediate Accounting 1B Foothill College, Summer 2015

(Part 2) Lunch Block 7 1:05 PM 2:27 PM

Accounting : Accounting Information Systems and Controls. Fall 2015 COLLEGE OF BUSINESS AND INNOVATION

ACTG 051A: Intermediate Accounting 1A Foothill College, Summer 2015

FNCE 3498: Accelerated Fundamentals of Finance Loyola Marymount University Spring 2015 Section 01: Tuesday & Thursday, 3:00pm-4:40pm, Room: Hilton 063

Criminology CRJU 2004 B Department of Criminal Justice College of Arts & Letters University of North Georgia

How can I keep track of the money in my checking account?

EASTERN WYOMING COLLEGE Business Administration

Drug Delivery Systems III

Brazosport College Syllabus for Criminology (CJSA 1325) Spring 2014 (Online)

Strategic Use of Information Technology (CIS ) Summer /

BI122 Introduction to Human Genetics, Fall 2014

How to be Successful in Foundations of Physics I (PHYS 2305)

Transcription:

Lecture 15: Inheritance 2/27/2015 Guest Lecturer: Marvin Zhang Some (a lot of) material from these slides was borrowed from John DeNero.

Announcements Homework 5 due Wednesday 3/4 @ 11:59pm Project 3 due Thursday 3/12 @ 11:59pm Midterm 2 on Thursday 3/19 7pm- 9pm Quiz 2 released Wednesday 3/4 Due Thursday 3/5 @ 11:59pm Object- oriented programming Similar to homework 5 Guerrilla section this Sunday 3/1 on mutation

Inheritance Powerful idea in Object- Oriented Programming Way of relating similar classes together Common use: a specialized class inherits from a more general class class <new class>(<base class>):... The new class shares attributes with the base class, and overrides certain attributes Implementing the new class is now as simple as specifying how it s different from the base class

Inheritance Example class Account: """A bank account."""... Bank accounts have: an account holder a balance an interest rate of 2% You can: deposit to an account withdraw from an account class CheckingAccount(Account): """A checking account."""... Checking accounts have: an account holder a balance an interest rate of 1% a withdraw fee of $1 You can: deposit to a checking account withdraw from a checking account (but there s a fee!)

Inheritance Example class Account: """A bank account."""... Bank accounts have: an account holder a balance an interest rate of 2% You can: deposit to an account withdraw from an account class CheckingAccount(Account): """A checking account."""... Checking accounts have: an account holder a balance an interest rate of 1% a withdraw fee of $1 You can: deposit to a checking account withdraw from a checking account (but there s a fee!)

Inheritance Example class Account: """A bank account."""... Bank accounts have: an account holder a balance an interest rate of 2% You can: deposit to an account withdraw from an account class CheckingAccount(Account): """A checking account."""... Checking accounts have: an account holder a balance an interest rate of 1% a withdraw fee of $1 You can: deposit to a checking account withdraw from a checking account (but there s a fee!)

Inheritance Example (demo) class Account: """A bank account."""... Bank accounts have: an account holder a balance an interest rate of 2% You can: deposit to an account withdraw from an account class CheckingAccount(Account): """A checking account."""... Checking accounts have: an account holder a balance an interest rate of 1% a withdraw fee of $1 You can: deposit to a checking account withdraw from a checking account (but there s a fee!)

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Attribute Look Up To look up a name in a class: 1. If the name is in the attributes of the class, return the corresponding value 2. If not found, look up the name in the base class, if there is one Base class attributes are not copied into subclasses! >>> tom = CheckingAccount('Tom') # Account. init >>> tom.interest # Found in CheckingAccount 0.01 >>> tom.deposit(20) # Found in Account 20 >>> tom.withdraw(5) # Found in CheckingAccount 14

Designing for Inheritance Don t repeat yourself! Use existing implementations Reuse overridden attributes by accessing them through the base class Look up attributes on instances if possible class CheckingAccount(Account): withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw( self, amount + self.withdraw_fee)

Inheritance vs Composition (demo) Inheritance: relating two classes through specifying similarities and differences Represents is a relationships, e.g. a checking account is a specific type of account Composition: connecting two classes through their relationship to one another Represents has a relationships, e.g. a bank has a collection of bank accounts

Multiple Inheritance In Python, a class can inherit from multiple base classes This exists in many but not all object- oriented languages This is a tricky and often dangerous subject, so proceed carefully! class SavingsAccount(Account): deposit_fee = 2 def deposit(self, amount): return Account.deposit( self, amount - self.deposit_fee)

Multiple Inheritance Example Bank executive wants the following: Low interest rate of 1% $1 withdrawal fee $2 deposit fee A free dollar for opening the account! class BestAccount(CheckingAccount, SavingsAccount): def init (self, account_holder): self.holder = account_holder self.balance = 1 # best deal ever

Multiple Inheritance Example Account CheckingAccount SavingsAccount BestAccount >>> such_a_deal = BestAccount('Marvin') >>> such_a_deal.balance # instance attribute 1 >>> such_a_deal.deposit(20) # SavingsAccount 19 >>> such_a_deal.withdraw(5) # CheckingAccount 13

Multiple Inheritance Example Account CheckingAccount SavingsAccount BestAccount >>> such_a_deal = BestAccount('Marvin') >>> such_a_deal.balance # instance attribute 1 >>> such_a_deal.deposit(20) # SavingsAccount 19 >>> such_a_deal.withdraw(5) # CheckingAccount 13

Multiple Inheritance Example Account CheckingAccount SavingsAccount BestAccount >>> such_a_deal = BestAccount('Marvin') >>> such_a_deal.balance # instance attribute 1 >>> such_a_deal.deposit(20) # SavingsAccount 19 >>> such_a_deal.withdraw(5) # CheckingAccount 13

Multiple Inheritance Example Account CheckingAccount SavingsAccount BestAccount >>> such_a_deal = BestAccount('Marvin') >>> such_a_deal.balance # instance attribute 1 >>> such_a_deal.deposit(20) # SavingsAccount 19 >>> such_a_deal.withdraw(5) # CheckingAccount 13

Complicated Inheritance To show how complicated inheritance can be, let s look at an analogy through biological inheritance. some guy Gramma Gramps Grandpop Grandmom some other guy Double Half Aunt Mom Dad Double Half Uncle Quadruple Double Half Cousin You Moral of the story: inheritance (especially multiple inheritance) is complicated and weird. Use it carefully!