>>     < >




In-class notes for 04/25/2014

CS 121B (CS1), Spring 2014

  • Homework assignment

  • 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

Upcoming

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 a TradeBook class, or a Tree 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:

    Superclass Account spec, code

    Subclass IntAccount spec, code

  • 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 of Account, plus a state variable totalInt (indicating how much interest has been earned so far) and two relevant methods getTotalInt() and addInterest(). A spec for IntAccount does not mention the state variables and methods of Account, such as balance and deposit() -- those appear already in the spec for Account. Only the new state variable and methods appear in the IntAccount spec, plus a spec for the constructor IntAccount().

  • Programming subclasses. The code for defining the subclass IntAccount provides only the code for the new state variable totalInt, the constructor IntAccount(), 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 variables balance and initBal and the four methods of an Account for this new class IntAccount, through inheritance.

  • In the __init__() method for IntAccount, those inherited state variables balance and initBal 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, the Account 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 subclass IntAccount 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 variable balance inherited from Account.

    • self.totalInt acccesses the state variable totalInt of IntAccount

    • self.deposit() accesses the method deposit() inherited from Account.

  • 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

Relationships between classes

  • Aggregation, e.g., Garden, which includes a list of Plant

    "HAS A" relationship: A Garden object has a Plant object (in fact, has several of them)

  • Inheritance e.g., IntAccount is a subclass of Account

    "IS A" relationship: A IntAccount object is an Account object




< >