Main Content
The Light bulb moment when people see how Object Orientation works in real use Archive - Originally posted on "The Horse's Mouth" - 2010-05-28 07:55:12 - Graham Ellis
A big "light bulb" moment for many customers is when they "get" object inheritance. And one of the best ways of explaining it is by writing an example.
On Wednesday, I wrote an example during our Python Course - taking a customer of Well House Consultants as my base class, then defining two extended classes (i.e. subtypes with extra and amended features) called "resident" and "delegate". See [source] .
In my example, all customers have names and a length of stay, but the price algorithm differs in the subclasses. I've also redefined the "+" operator, so that if I add two residents together they end up sharing a room, and if I add a number to a resident, that extends their stay by that number of nights.
When I call up the name of a customer, it runs the same code for any customer, but when I call up the price for a customer, it runs different code depending on whether it's a delegate or a resident. This is "Polymorphism"
Python is very neat in that I can define all of the classes in a single file (A Java example would have had three or 4 files of source), and it's much cleaner than objects in Perl. Which is why Python is such a superb language for researchers.
Some other articles
Y112 - Objects - Intermediate Nesting decorators Defining an object that is a modified standard type in Python with in Python - examples of use, and of defining your own context Object and Static methods - what is the difference; example in Python 3 Setting up and tearing down with the Python with keyword Deciding whether to use parameters, conditional statements or subclasses Spike solution, refactoring into encapsulated object methods - good design practise A good example of recursion - a real use in Python Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Python base and inherited classes, test harness and unit testing - new examples Python Properties - how and why Really Simple Class and Inheritance example in Python Inheritance, Composition and Associated objects - when to use which - Python example Backquote, backtic, str and repr in Python - conversion object to string Metaclasses (Python) and Metatables (Lua) Static variables in functions - and better ways using objects A demonstration of how many Python facilities work together A list of special method and attribute names in Python Python - some common questions answered in code examples Defining static methods in Python Should Python classes each be in their own file? This article Python decorators - your own, staticmethod and classmethod Mixins example in Python Multiple inheritance in Python - complete working example The Multiple Inheritance Conundrum, interfaces and mixins Methods that run on classes (static methods) in Python How do I set up a constant in Python? TypeError: super() argument 1 must be type, not classobj (Python) Python - fresh examples of all the fundamentals Calling base class constructors Equality, sameness and identity - Python Using a utility method to construct objects of different types - Python Python - formatting objects What are factory and singleton classes? __new__ v __init__ - python constructor alternatives? Practical polymorphism in action Pieces of Python Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML Class, static and unbound variables Overloading of operators on standard objects in Python Using a Python dictionary as a holder of object attributes Q907 - Object Orientation: Design Techniques Associative objects - one object within another. Spike solution, refactoring into encapsulated object methods - good design practise The spirit of Java - delegating to classes Test driven development, and class design, from first principles (using C++) Using object orientation for non-physical objects Teaching OO - how to avoid lots of window switching early on Storing your intermediate data - what format should you you choose? Inheritance, Composition and Associated objects - when to use which - Python example From Structured to Object Oriented Programming. Rooms ready for guests - each time, every time, thanks to good system design When you should use Object Orientation even in a short program - Python example Spike solutions and refactoring - a Python example Why you should use objects even for short data manipulation programs in Ruby Designing your application - using UML techniques Your PHP website - how to factor and refactor to reduce growing pains Ruby - a training example that puts many language elements together to demonstrate the whole Object Oriented Programming for Structured Programmers - conversion training Comments in and on Perl - a case for extreme OO programming What is a factory method and why use one? - Example in Ruby Turning an exercise into the real thing with extreme programming Should Python classes each be in their own file? Program for reliability and efficiency - do not duplicate, but rather share and re-use Relationships between Java classes - inheritance, packaging and others This article Containment, Associative Objects, Inheritance, packages and modules What is a factory? The Multiple Inheritance Conundrum, interfaces and mixins Plan your application before you start Simples Object Oriented programming - a practical design example Planning! Designing a heirarcy of classes - getting inheritance right When should I use OO techniques? Teaching Object Oriented Java with Students and Ice Cream Object Oriented Tcl Object Oriented Programming in Perl - Course Object Relation Mapping (ORM) What are factory and singleton classes? Maintainable code - some positive advice Build on what you already have with OO Comparison of Object Oriented Philosophy - Python, Java, C++, Perl The Fag Packet Design Methodology Think about your design even if you don't use full UML Design - one name, one action Introduction to Object Oriented Programming Tapping in on resources OO - real benefits