User Guide
Table Of Contents
146
contains the same basic information (name, phone number, and
so on), it can inherit those fields from the standard entry object.
But then you can add other fields that are specific to business
contacts (such as company name and website).
class AddressBookEntry:
firstname = ""
lastname = ""
telephone = ""
def __init__ (self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
def printName(self):
print(self.firstname + " " + self.lastname)
class BusinessContactEntry(AddressBookEntry):
company = ""
Create an instance of BusinessContactEntry using the syntax:
Bob =
BusinessContactEntry(firstname="Bob",lastname="Smith")
Even though the BusinessContactEntry class only defines the
field company, the fields firstname, lastname, and telephone are
also usable. It inherits these fields from AddressBookEntry.
Bob.firstname = "Bob"
Bob.company = "Bob’s Widgets, Inc."
Bob.printName()
This relationship does not work the other way. Since
AddressBookEntry does not inherit from BusinessContactEntry,
you cannot access the company field from an instance of
AddressBookEntry.
Overriding Inherited Methods
In the example above, the BusinessContactEntry class inherits
the printName() method from AddressBookEntry. However, if
you include a new definition of __init__ and printName() in