Class Notes (808,548)
Canada (493,287)
CSC148H1 (92)
Paul Gries (18)


2 Pages
Unlock Document

University of Toronto St. George
Computer Science
Paul Gries

 objects interact with each other in one of three ways: o Association: an object is aware of another object and holds a reference to it o Composition: objects combining to create more complex ones o Inheritance: classes (types) are defined as extensions of other classes  Association: In an associative has-a relationship, an object is aware of another complex object and can communicate with it o ex. a Car has an owner attribute which is a Person  Composition: In a compositional has-a relationship, an object is made up of other objects o ex. a Movie object might be composed of str objects title and genre and int object year.  Interitance: is-a relationship o It means that objects of a particular class are members of a subset of the objects of another class o The subclass inherits all the properties of the superclass, and adds some of its own o ex. Person class  Several modules may use this class to keep track of Person objects  Now, imagine that the university would like to use the Person class to store information about its students. 1. class Person(): 2. def __init__(self, n, y, g): 3. = n 4. self.year = y 5. self.gender = g 6. def __str__(self): 7. return "%s (%s) b. %s" %(, self.gender, self.year) o ex. Student class  The Person class does not have all the attributes necessary to keep track of a student's personal information.  We could add what we need to Person, which, if done by all other methods that may be using Person, would make Person a very long, unwieldy class.  Alternatively, we can create the parts that we're missing (student number, GPA, etc.) in another class and connect it to the Person class somehow. 1. class Student(???): 2. def __init__(self, stn, avg): 3. self.student_number = stn 4. self.gpa = avg o Now, this Student class also needs a name, a gender and a year of birth. We have three options: o Student Class A:  This option makes all the Person functionality available in the Student class  but it has a drawback: if a new attribute needed to be added for all people, it would have to be added in two places  Also, if improvements are made to Person, they are not automatically done in Student 1. class Student(object): 2. def __init__(self, n, y, g, s, a): 3. = n 4. self.year = y 5. self.gender = g 6. self.student_number = s 7. self.gpa = a o Student Class B:  use composition: this option makes the Student class store a Person object  This way, looking for the Student's name would involve checking its person attribute's name.  However, this is counter-intuitive as a metaphor since the student is not a separate entity from the person  It's not that students HAVE people, students ARE people. 1. class Student(object): 2. def __init__(self, n, y, g, s, a): 3. = n 4. self.year = y 5. self.gender = g 6. self.student_number = s 7. self.gpa = a o Student Class C:  use inheritance:
More Less

Related notes for CSC148H1

Log In


Don't have an account?

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.