Nice metaphor for object-oriented concepts

A friend has recently asked if I could write an interactive app to help him teach his students basic object-oriented concepts (e.g. classes vs objects, encapsulation, inheritance, etc).

To cut a long story short, I’m not too worried about the coding side or making sure that the theory is all sound (and useful). But I can’t quite decide on a suitable metaphor.

I’ve seen many examples demonstrating inheritance with animals or polygons. And for designing a whole OO app, I’ve seen examples of managing a (book) library, say.

But these are all quite dry and I’m struggling to think of something a bit more engaging.


If I use any suggestions from this thread, I’ll credit the suggestor accordingly. Also, I’ll post my app here. :slight_smile:

Finally, if anyone knows of any nice interactive apps already out there, by all means let me know. I’m helping my friend out partly to get a nice reference, but I’m not going to reinvent the wheel just to prove I can.

What age students?

I personally like the animal/zoo metaphor.

They are 16. It’s basically their first exposure to OO, and apparently many are struggling to get it.
Probably it isn’t helping that the language they are primarily teaching with is VB…but that’s another matter.

I recall my first exposure to OO: entirely abstract smalltalk examples. shudder
It made OO seem like a monumental hassle for no gain at all.

Hmm, I guess thinking about it, it does demonstrate the usefulness of OO in a fairly engaging way.
You don’t get a credit for this though :smiley:

I like the animal paradigm, specifically dogs, because they come in so many breeds. Makes it easy to explain polymorphism and such. I’m guessing you could use dog tricks do demonstrate interfaces. I don’t think that would be dry at all, you could have a lot of fun with it.

When I learned about it, they used cars. Honestly, I’d advise against that because it still keeps it kind of mechanical and isn’t helpful for those who don’t know a whole lot about them. I think someone else tried to use something like a table, but it was so bad I don’t even remember how it related to anything. I think tdn’s suggestion is about as good as you’ll get because everyone by that age has a pretty intuitive understanding of a lot of the basic characteristics of various animals.

Failing that, hoping that anyone taking programming classes knows more about computers than the average person, I think it was one of my silly intro classes as an undergrad where the professor used that as an example. But, the usefulness of that sort of example all depends on the level, and since they’re 16, that may be no more helpful than the car example since I imagine half of them are in there because they like video games.

I was thinking that exact thing, except with dolphins. Tricks are functions.

I’ve never really grasped object-oriented programming, and I think it’s partly because everyone always tries to teach it by analogy. But the best analogy for a thing is always the thing itself. Show them examples of real code that really uses object-oriented programming, and which benefits from using it.

I should say, my background is actually in video games, but any examples I could think of (and certainly most of the code on my machine), would be way over the top for what they are trying to learn.

Even a card game or a gorilla / worms game…I think would get too bloated if they had to actually work, and I don’t think they’d be ideal for OO concepts.

Well, I’m going to try this, and use an analogy.

Given my own bad experience with first learning OO, I thought I’d begin with a brief explanation of why it exists at all: spaghetti code, reusability and that fleshing out classes can be a nice way to begin structuring a project.

Then the interactive movie, which will use a metaphor, will ask questions at particular points and show code excerpts.

At the end it should give some homework. I’m thinking some source code, with a couple of stubs that students must complete. If we stipulate that they must use frequent comments, it will make it harder for them to copy their friends’ code without understanding the concepts :wink:

I once had to teach a class on bit-wise programming, and used a Star Trek video game that I’d written years before as an example.

It might work for this as well. The first instantiation of a ship might have the properties of being a Federation ship, named Enterprise, with a heading of 020, and zero damage. The second might be a Federation ship named Kobyashi Maru, heading of 274. Then various instantiations of Klingon ships could show up. Weapons and shields could be functions. A destroyed ship is a destroyed object.

You could even show the beginning of that movie for those who are unfamiliar with the story. Because Og knows that most programmers are unfamiliar with Star Trek.

Cars are Best, because you can also show modularity. Chang
e out the engine etc

Since you already have a background in games, why not use Cards as your basic object? One problem with using cars or animals is that those objects can get complex quickly, whereas cards are pretty simple.

What function does a card have?

Maybe a poker hand or something, which could have a function of ranking among other hands depending on the various combinations of its properties (suits and ranks).

Electric grid

I’m starting to like the poker idea, actually. You can create 2-8 objects, each one representing a hand. Each hand has 10 properties, 5 for rank and 5 for suit. A function within the object will score the hand. Compare the ranks and see if there are any duplicates or sequences. Then compare the suits to see if they are all the same. The function will return a score wich can be compared to the scores of the other hands.

You’d also need an object for the deck, which will have 52 property pairs, a function to shuffle them, and a function to deal.

I considered that.
It’s certainly an engaging concept, and it definitely shows encapsulation and container relationships.
But I’m not sure how it would show inheritance and polymorphism.


If I were to implement a poker game, for my own use, the GUI would involve some inheritance (naturally). And poker games would inherit from an abstract poker class, because it should be possible to implement different variants (5 card stud, texas hold 'em, etc) and reuse code.
None of this is a good way to show OO to absolute beginners however.

I’d just call it a black box that you can make tons of copies of.

Sounds like you had a good teacher :smiley:

Use the video game Call of Duty. Have them discuss what the objects should be and their inheritances. Let them come up with all the different messages/actions.

Speaking from personal experience, I didn’t really get OO until I started using it in a useful manner. IE the built in collection classes in Java.

Talking about Array List’s and Linked Lists and Trees and swapping them in and out of things.

Like passing them to a function that finds a specific item in the collection, or splits the collection into partitions. Or defining a function to behave like a collection to display encapsulation and polymorphism.
As far as analogies go, I think the best analogies to demonstrate OO not only focus on the polymorphism, encapsuation, and specification, but also how different objects interact with each other, aka utilization.

Do they already know programming? If so, then virtually any logical metaphor should be good enough, because they already know programming and just need to apply it to a different model.

If they don’t already know programming, then your task is much greater.