In-class notes for 04/25/2014
CS 121B (CS1), Spring 2014
CS talk on Monday at 3:30 in RNS 410:
Carl Albing, one of the first students to take CS at St. Olaf in mid-70's, now at Cray Research.Next quiz on Wednesday
Submitted questions on assignments and technology
-
Solution; demo on WebMapReduce
Review question from a prior time: HW18, Dictionary question 2a
Read the instructions preceding the question.
Files; dictionary as accumulator
Options:
int
vsstr
keys;accum[lis[0]]
vs intermediate variable namemovieID
Upcoming
Project: starter suggestions; your submitted ideas; in-class feedback last time
Next step: vote on your project interests (by Sunday night), electronic ballot, so I can form teams that connect people with similar interests. (Will gather your team preferences, too.)
Inheritance
- The takeaway messages:
Python3 makes it possible for objects in one class to receive automatically all the state variables and methods of another class.
This feature is known as inheritance.
Using inheritance means not having to reprogram the state variables and methods from one of the classes when defining the other class.
Examples:
InterestAccount
class, or aTradeBook
class, or aTree
class...Inheritance: when one class automatically receives (inherits) the state variables and methods of another.
Superclass: A class that provides inherited state variables and methods
Subclass: A class that receives (obtains?) inherited state variables and methods.
Examples:
Specs for subclasses. The spec for a subclass indicates the superclass, then only and the new state variables and methods for that subclass, together with a constructor for that subclass. For example, consider an interest-bearing bank account class
IntAccount
which has all the state variables and methods ofAccount
, plus a state variabletotalInt
(indicating how much interest has been earned so far) and two relevant methodsgetTotalInt()
andaddInterest()
. A spec forIntAccount
does not mention the state variables and methods ofAccount
, such asbalance
anddeposit()
-- those appear already in the spec forAccount
. Only the new state variable and methods appear in theIntAccount
spec, plus a spec for the constructorIntAccount()
.Programming subclasses. The code for defining the subclass
IntAccount
provides only the code for the new state variabletotalInt
, the constructorIntAccount()
, and the two new methods.The superclass is indicated in parentheses at the beginning of the subclass definition.
class IntAccount(Account):
This automatically defines the state variablesbalance
andinitBal
and the four methods of anAccount
for this new classIntAccount
, through inheritance.In the
__init__()
method forIntAccount
, those inherited state variablesbalance
andinitBal
must also be initialized. This is accomplished by calling the superclass's__init__()
method, using the following code in this case.Account.__init__(self, bal)
In this case, theAccount
constructor only requires one argument (bal
); in general, a superclass's constructor may require any number of arguments, and those would have to be included in a call of that superclass's__init__()
.Inherited state variables and methods can be accessed the same way as usual. For example, the
addInterest()
method of the subclassIntAccount
is defined as follows:def addInterest(self, annrate): int = annrate * self.balance / 12 self.totalInt = self.totalInt + int self.deposit(int) return self.balance
self.balance
accesses the state variablebalance
inherited fromAccount
.self.totalInt
acccesses the state variabletotalInt
ofIntAccount
self.deposit()
accesses the methoddeposit()
inherited fromAccount
.
Note: If a subclass overrides (defines it's own version of) a method in a superclass, then a call
self.method(...)
will access that subclass's version of that method, not the superclass's version. For overridden methods, this syntax will call a superclass's version of a method within the definition of a subclass method:Supercl.method(self, ...)
Example:IntAccount2.py
Exercises
Exercise 1:
Define a subclass
TradeBook
ofBook
that satisfies this spec.(Implementation of
Book
; shouldn't be needed)Exercise 2:
Relationships between classes
Aggregation, e.g.,
Garden
, which includes a list ofPlant
"HAS A" relationship: A
Garden
object has aPlant
object (in fact, has several of them)Inheritance e.g.,
IntAccount
is a subclass ofAccount
"IS A" relationship: A
IntAccount
object is anAccount
object
< >