0% found this document useful (0 votes)
42 views6 pages

Using Inheritance: - Let's Build An Application That Organizes Info About People!

The document discusses using inheritance to build a class hierarchy for organizing information about people at MIT. It defines classes like Person, MITPerson, UndergraduateStudent, and GraduateStudent, with MITPerson as a subclass of Person and the student classes as subclasses of MITPerson. The document shows how the hierarchy can be cleaned up and improved by introducing a Student superclass for the different student subclasses to inherit from, rather than having them all inherit directly from MITPerson. This allows common student behaviors to be defined in a single place.

Uploaded by

petejuan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views6 pages

Using Inheritance: - Let's Build An Application That Organizes Info About People!

The document discusses using inheritance to build a class hierarchy for organizing information about people at MIT. It defines classes like Person, MITPerson, UndergraduateStudent, and GraduateStudent, with MITPerson as a subclass of Person and the student classes as subclasses of MITPerson. The document shows how the hierarchy can be cleaned up and improved by introducing a Student superclass for the different student subclasses to inherit from, rather than having them all inherit directly from MITPerson. This allows common student behaviors to be defined in a single place.

Uploaded by

petejuan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Using Inheritance

Let s build an application that organizes info about people!


Person: name, birthday
Get last name Sort by last name Get age

MITPerson: Person + ID Number


Assign ID numbers in sequence Get ID number Sort by ID number

Students: several types, all MITPerson


Undergraduate student: has class year Graduate student

More classes for the hierarchy


class UG(MITPerson): def __init__(self, name, classYear): MITPerson.__init__(self, name) self.year = classYear

def getClass(self): return self.year


class Grad(MITPerson): pass def isStudent(obj): return isinstance(obj,UG) or isinstance(obj,Grad)

Class Hierarchy & Substitution Principle


Person

Heres a diagram showing our class hierarchy

MITPerson

UG

Grad

Subclass

Superclass

Cleaning up the hierarchy


class UG(MITPerson): def __init__(self, name, classYear): MITPerson.__init__(self, name) self.year = classYear def getClass(self): return self.year class Grad(MITPerson): pass class TransferStudent(MITPerson): pass Now I have to rethink isStudent

def isStudent(obj): return isinstance(obj,UG) or isinstance(obj,Grad)

Class Hierarchy & Substitution Principle


Person

MITPerson

Heres a diagram showing our class hierarchy Be careful when overriding methods in a subclass!
Substitution principle: important behaviors of superclass should be supported by all subclasses

Student

UG

Grad

Transfer

Subclass

Superclass

Cleaning up the hierarchy


class Student(MITPerson): pass class UG(Student): def __init__(self, name, classYear): MITPerson.__init__(self, name) self.year = classYear def getClass(self): return self.year class Grad(Student): pass class TransferStudent(Student): pass

Better is to create a superclass that covers all students In general, creating a class in the hierarchy that captures common behaviors of subclasses allows us to concentrate methods in a single place, and lets us think about subclasses as a coherent whole

def isStudent(obj): return isinstance(obj,Student)

You might also like