Introduction
In the previous post we looked at interface polymorphism in Python. In fact there are no explicit interfaces in Python like in Java so I simply made up the term “interface polymorphism” referring to strictly typed languages.
We saw an example of two methods: one that printed the provided data in XML format, and another which performed the same in JSON format. The print_as_xml and print_as_json functions, which are also objects, implicitly implemented an interface with one method. This method is void, i.e. has no return type, and accepts two input parameters: a root name and the properties to be printed arranged in a dictionary. The Person object then had a format_me method which accepted a formatter and delegated the actual formatting implementation details to the mechanism that was passed into it. It’s important to keep in mind that the Person object remained oblivious of these implementation details and didn’t need to concern itself with the rules concerning JSON, XML, etc. Also if we pass in a function that doesn’t adhere to the implicit interface then an exception is thrown.
In this post we’ll start looking into another key idea in object-oriented design: inheritance. We’ll start with some theory and key terms. The upcoming posts will show the ideas in action.
Read more of this post