teach_cs_toronto_edu_csc148h_notes_inheritance_inheritance_design_html
teach_cs_toronto_edu_csc148h_notes_inheritance_inheritance_design_html
2.1 Testing Your Work Of course, we should never do this when the method is abstract, because a subclass must override every abstract method to
5. Exceptions subclass initializer calls it as a helper and then has additional code to initialize additional instance attributes that are specific to
that subclass.
5.1 Introduction to Exceptions
5.2 General Rules for try-except We can extend any inherited method, not just an initializer. Here’s an example. Suppose at pay time we wanted to print out two
5.3 Why Not Just Return a Special messages, the original one from Employee , and also a SalariedEmployee -specific message. Since we already have a
Value? superclass method that does part of the work, we can call it as a helper method instead of repeating its code:
7. Recursion
7.2 Nested Lists: A Recursive Data Using inheritance to define a shared public interface
Structure
Our use of inheritance allows client code to do the same thing for all types of employee. Here’s an example where we iterate
7.3 Understanding Recursive
over a list of employees and call method pay on each, without regard to what kind of Employee each one is:
Functions: Partial Tracing
8.5 Binary Search Tree We say that the Employee class represents the shared public interface of classes SalariedEmployee and
Implementation and Search HourlyEmployee . The public interface of a class is the way client code interacts with the methods and attributes of the class.
8.6 Mutating Binary Search Trees It’s a “shared” public interface in the sense that it is held in common between SalariedEmployee and HourlyEmployee .
Our abstract Employee class is useful in a second way. If and when someone does decide to write another subclass of
Employee , for instance for employees who are paid a commission, the programmer knows that the abstract method
get_monthly_payment must be implemented. In other words, they must support the shared public interface that the client
code counts on. We can think of this as providing helpful guidance for the programmer writing the new subclass.
Composition is commonly thought of as a “has a” relationship. For example, a person “has a” car. Inheritance is thought of as
an “is a” relationship. For example, a salaried employee “is an” employee. Of course, the “has a” vs. “is a” categorization is
rather simplistic, and not every real-world problem is so clearly defined.
When we use inheritance, any change in a superclass affects all of its subclasses, which can lead to unintended effects. To
avoid this complexity, in this course we’ll stick to using inheritance in the traditional “shared public interface” sense. Moreover,
we will often prefer that a subclass not change the public interface of a superclass at all:
not by changing the interface of any public methods (e.g., adding/removing parameters, or changing their types)
not by adding new public methods or attributes to a subclass (of course, adding private attributes or methods is
acceptable)
As a general programming concept, inheritance has many other uses, and you’ll learn about some of them in CSC207,
Software Design.
[1] This is not a requirement of the Python language, but is a feature of the way we are using inheritance. In other uses of
inheritance, implementation can be deferred to a subclass of the subclass or a class further down in the inheritance chain.
[2] The roots of this word are poly, which means “many”, and morphe, which means “form”. So “polymorphic” literally means
“taking many forms”.
Previous Next
3.7 Inheritance: Tracing Initialization 3.9 The object Class and Python Special
Methods