Visualizations: Interfaces

Using interfaces are very hard for beginners in programming and even harder for those who have programmed in a language that does not have them to understand.

In the middle of class, in desparation, I whipped out my keys and struck on a very good visualization for why we use just the interface and not the instance class in many situation.

Materials: 3 clip-on key lanyards with different names (I even have one with the school name on it, this fits best) and two collections of keys.

Preparation: put all three lanyards on both collections of keys at the same time. Call them: work, home, and mine. Mine has both of the collections of keys, work the office keys, home my home keys and the car keys.

Explaination: [Hold up the keys with just one of the lanyards, mine] These are my keys. I have a lot of them. My instance type is MyKeys and I have keys for work, for my apartment, for my car, and other stuff. We could think of these as methods that do something. Now, when I am at work, I have no use for my home keys. [Now pick it up by with work lanyard.] It is sufficient - and better - for me to just use the keys using this lanyard. It lets me access all the methods (i.e. keys) that I need in this circumstance. The other keys just do not make sense here. They are, however, available and still on the chain, but I am just using the work keys. [Pick up the home lanyard] Now, when I want to drive home, the work keys make no sense, so I just want to access the home ones. I still have all of the keys on the object. But the current context - driving home - only needs a few of the keys that I acutally possess.

This was first tried in Juni 2007 - and seemed to make a lot of sense to people. We'll see on the next exercise!

 

 


Debora Weber-Wulff <weberwu@htw-berlin.de>