Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What are some good examples for illustrating the concepts of object-oriented programming? I'm talking about sample classes, classes that would have inheritance, overriding, etc.

The only one I can ever think of is of animals. A base Animal class, with Cow that have a different makeCry method or something like that. It sucks. What do you guys use?


locked by maple_shaft May 17 at 0:52

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as off-topic by gnat, BЈовић, GlenH7, Martijn Pieters, Robert Harvey Jul 22 '13 at 15:38

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

Are you actually trying to teach this to new programmers, old programmers without OOP experience, or non-programmers? –  FrustratedWithFormsDesigner Aug 11 '11 at 16:54
No, I wish that was my job :). I'm only curious as to what others use. –  Xeon06 Aug 11 '11 at 17:03
OOP prism is not the best object to look through at the real world. –  Job Aug 11 '11 at 19:26
my favorite example for illustrating the concepts of object-oriented programming is POS system case study in Larman's Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design... –  gnat Jul 20 '13 at 20:52
This question reads as a poll; there isn't any means to differentiate one answer as better than another. Polls are not a good fit for the StackExchange Q&A format. –  GlenH7 Jul 21 '13 at 17:16

4 Answers 4

up vote 3 down vote accepted

The classic set of objects I've always seen is Vehicle (base), Car, Truck, Airplane.

Edited to expand on the objects.

Vehicle could have a method numberOfWheels, which returns an integer. The Car numberOfWheels would return 4. The Truck numberOfWheels could return 4, 6, 10, or 18, depending on the type of truck. The Airplane numberOfWheels would return 17 for a jet airliner (I think).

Another method could be engineType, which returns a String. The Car engineType would return either "gasoline" or "diesel". The Truck engineType would return either "gasoline" or "diesel". The Airplane engineType would return either "gasoline" or "jet".

You could certainly come up with more methods that vehicles have in common.

What are some methods and properties? What is overloaded? –  Xeon06 Aug 11 '11 at 16:40
Methods (Stop(), Start(), Accelerate(), etc) each would have a different implementation. Throw in a sail boat to really mess things up... –  CaffGeek Aug 11 '11 at 19:44

Off the top of my head: You're making a game, something like SimCity.

You've got a base class called building, and subclasses called residential, commercial, utility, or whatnot.

All buildings have a location, value, and land-tax (you're mayor remember).
Residential buildings provide living space.
Commercial buildings provide working space.
Utility buildings provide services.

The calculateValue and calculateLandTax functions would be handled differently for residential and commercial buildings.


I once had to teach children these concepts. The projects were mostly simple video-games which lent itself very nicely to basic OOD/OOP. You had Sprite which was a root of a hierarchy. It would have attributes such as size, speed, and health and these would often be overriden to have different values in different subclasses. Common methods that all Sprites had (but sometimes overrode) would be things like move. Then you had subclasses such as GoodGuy, BadGuy, and sometimes we'd have a Scenery class (for sprites that just moved around in the background). Sometimes BadGuy would have further subclasses such as BossBadGuy and BigBossBadGuy which would override some of the BadGuy methods (such as attack, die, respawn).


The best example I know of is actors on a stage in a play - it also helps you avoid ending up in the inheritance tar pit.

Say we have the play Julius Caesar from William Shakespeare. You could implement it with a bunch of Actor classes, from which e.g. the Calpurnia class is derived. You could have multiple Calpurnia classes (say for different actresses knowing the role of Calpurnia, each with different strength an weaknesses, but all of them able to play - more or less - properly their role).

When during a play of Julius Caesar Calpurnia says the line:

What mean you, Caesar? think you to walk forth? You shall not stir out of your house to-day.

The instance of the Calpurnia class (= object) sends a message (= calls a method on) an instance of the Julius Caesar class. Which reacts according to the script (= algorithm implemented in the called method) with the line:

The things that threaten'd me ne'er look'd but on my back; when they shall see The face of Caesar, they are vanished.

The instance of the Calpurnia class though has no idea how the Caesar object creates this answer (= encapsulation) and everyone is perfectly aware, that this is not the real Julius Caesar (or Calpurnia) standing on the stage, but actors representing reasonably romanticized versions of the historic persons (= abstraction).

Another participating class in this scenario is the Spectator class. They make heavy use of the fact, that all roles belong to the Actor class and as such instances of them can be watched, listened too or applauded – or booed – irrespective of their concrete subclass (= polymorphism).

And of course there are thousands of different ways on how'd you be able to represent a play(house) in an object oriented fashion...


Not the answer you're looking for? Browse other questions tagged or ask your own question.