CS61A Lecture 16: Inheritance

4 Pages
Unlock Document

Computer Science
De Nero

Attributes All objects have attributes, which are name-value pairs Classes are objects too, so have attributes Instance attribute: attribute of an instance instance = created by calling class Class attribute = attribute of class of an instance Terminology: Method = Class Attribute that has Function Python Object System: Functions are object Bound methods are object a function has first parameter as "self" already bound to instance Dot expression evaluate to bound method for class attributes that are functions . Attribute Assignment Assignment statements with dot expression on left side affect attributes for object of dot expression If the object is an instance, then assignment sets an instance attribute If the object is a class, then assignment sets a class attribute Example: tom_account.interest = 0.08 Attribute assignment statement adds or modifies attribute name interest of tom_account expression tom_account eval to object, interest not looked up Example: Account.interest = 0.08 class attribute assignment Inheritance Method for relating classes together A common use: two similar classes differ in degree of specialization The specialized class may have same attributes as general class, along with some special behavior class (): New subclass shares attributes with base class The subclass may override certain inherited attributes Using inheritance, implement subclass by specify differences from base class Example: A CheckingAccount is specialized type of Account Ch = CheckingAccount('Tom') ch.interest = 0.01, lower interest rate ch.deposit(20) deposit same ch.withdraw(5) withdraw incur $1 fee Most behavior share with base class Account class CheckingAccount(Account): withdraw fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw(self, amount+self.withdraw_fee) Base class attributes aren't copied into subclasses To look up name i
More Less

Related notes for COMPSCI 61A

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.