6 01 Spring Semester 2008 Course notes for Week 4 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6 01 Introduction to EECS I Spring Semester 2008 Course notes for Week 4 1 State objects and abstraction Last week we introduced object oriented programming motivating classes because they provide a convenient way of organizing the procedures and data associated with an abstract data type This week we ll look at some other important abstractions strategies for computer programs and see how OOP can help us with them as well In the context of our table and the PCAP framework we will pay special attention to generic functions and inheritance in OOP which give us methods for capturing common patterns in data and the procedures that operate on that data Primitives Means of combination Means of abstraction Means of capturing common patterns 1 1 Procedures if while f g x def higher order procedures Data numbers strings lists dictionaries objects abstract data types classes generic functions inheritance Objects and state We saw last week how to define a convenient bank account object that implements an ADT for a bank account class Account def init self initialBalance self currentBalance initialBalance def balance self return self currentBalance def deposit self amount self currentBalance self currentBalance amount def creditLimit self return min self currentBalance 0 5 10000000 We can use this ADT to create and manage several bank accounts at once a Account 100 b Account 1000000 100 100 Account balance a a balance Account deposit a 100 a deposit 100 6 01 Spring Semester 2008 Course notes for Week 4 2 a balance 300 b balance 1000000 a balance 300 The Account class contains the procedures that are common to all bank accounts the individual objects contain state in the values associated with the names in their environments That state is persistent in the sense that it exists for the lifetime of the program that is running and doesn t disappear when a particular method call is over 1 2 Generic functions Now imagine that the bank we re running is getting bigger and we want to have several different kinds of accounts Now there is a monthly fee just to have the account and the credit limit depends on the account type Let s see how it would work to use dictionaries to store the data for our bank accounts Here s a new data structure and two constructors for the different kinds of accounts def ma ke Pr emi er Ac co un t balance rate owner ssn return balance balance interestRate rate owner owner ssn ssn type Premier def ma ke Ec ono my Ac co un t balance rate owner ssn return balance balance interestRate rate owner owner ssn ssn type Economy a5 mak eP re mi er Ac co un t 3021835 97 0003 Susan Squeeze 558421212 a6 mak eE co no my Ac co un t 3 22 00000001 Carl Constrictor 555121348 The procedures for depositing and getting the balance would be the same for both kinds of accounts But how would we get the credit limit We could have separate procedures for getting the credit limit for each different kind of account def cr ed it Lim it Ec on om y account return min account balance 0 5 20 00 def cr ed it Lim it Pr em ie r account return min account balance 1 5 10000000 c re di tL im it Pr em ie r a5 45 32 75 3 95 50 00 00 01 c re di tL im it Ec on om y a6 1 61 00 00 00 00 00 00 01 But doing this means that no matter what you re doing with this account you have to be conscious of what kind of account it is It would be nicer if we could treat the account generically We can 6 01 Spring Semester 2008 Course notes for Week 4 3 by writing one procedure that does different things depending on the account type This is called a generic function def creditLimit account if account type Economy return min balance 0 5 20 00 elif account type Premier return min balance 1 5 10000000 else return min balance 0 5 10000000 creditLimit a5 45 32 75 3 95 50 00 00 01 creditLimit a6 1 61 00 00 00 00 00 00 01 In this example we had to do what is known as type dispatching that is we had to explicitly check the type of the account being passed in and then do the appropriate operation We ll see later in this lecture that Python has the ability to do this for us automatically 1 3 Classes and inheritance If we wanted to define another type of account as a Python class we could do it this way class PremierAccount def init self initialBalance self currentBalance initialBalance def balance self return self currentBalance def deposit self amount self currentBalance self currentBalance amount def creditLimit self return min self currentBalance 1 5 10000000 c PremierAccount 1000 c creditLimit 1500 0 This will let people with premier accounts have larger credit limits And the nice thing is that we can ask for its credit limit without knowing what kind of an account it is so we see that objects support generic functions as we spoke about them earlier However this solution is still not satisfactory In order to make a premier account we had to repeat a lot of the same definitions as we had in the basic account class That violates our fundamental principle of laziness never do twice what you could do once instead abstract and reuse Inheritance lets us make a new class that s like an old class but with some parts overridden or new parts added When defining a class you can actually specify an argument which is another class You are saying that this new class should be exactly like the parent class or superclass but with certain definitions added or overridden So for example we can say class PremierAccount Account def creditLimit self return min self currentBalance 1 5 10000000 6 01 Spring Semester 2008 Course notes for Week 4 4 class EconomyAccount Account def creditLimit self return min self currentBalance 0 5 20 00 a Account 100 b PremierAccount 100 c EconomyAccount 100 a creditLimit 100 0 b creditLimit 150 0 c creditLimit 20 0 This is like generic functions But we don t have to define the whole thing at once We can add pieces and parts as we define new types of accounts And we automatically inherit the methods of our superclass including init So we still know how to make deposits into a premier account b deposit 100 b balance 200 This is actually implemented by setting the subclass s enclosing environment to be the superclass s environment Figure 1 shows the environments after we ve executed all of the account related statements above You can see that each class and each instance is an environment
View Full Document