views:

1200

answers:

17

My relative is studying programming and has a hard time understanding classes. He has trouble understanding for example that you need to instantiate it, that methods cannot access variables in other methods and if you change a variable in one instance of a class it doesn't change for other instances.

I've tried to use analogies like a class definition is like a blueprint of a house. And instances are houses made from that blueprint.

How do you explain classes and OO in general?

+16  A: 

Seriously use Animals, it works great. And that's what nailed the concept for me years ago. Just found this C# code. It seems good

 // Assembly: Common Classes
 // Namespace: CommonClasses

 public interface IAnimal
 {
     string Name
     { 
          get; 
     }
     string Talk();
 }

 // Assembly: Animals
 // Namespace: Animals

 public class AnimalBase
 {
     private string _name;
     AnimalBase(string name)
     {
        _name = name;
     }
     public string Name
     {
        get
        {
           return _name;
        }
     }
 }

 // Assembly: Animals
 // Namespace: Animals

 public class Cat : AnimalBase, IAnimal
 {
     public Cat(String name) :
         base(name)
     {
     }

     public string Talk() {
         return "Meowww!";
     }
 }

 // Assembly: Animals
 // Namespace: Animals

 public class Dog : AnimalBase, IAnimal
 {
     public Dog(string name) : 
         base(name)
     {
     }

     public string Talk() {
         return "Arf! Arf!";
     }
 }

 // Assembly: Program
 // Namespace: Program
 // References and Uses Assemblies: Common Classes, Animals

 public class TestAnimals
 {
     // prints the following:
     //
     // Missy: Meowww!
     // Mr. Bojangles: Meowww!
     // Lassie: Arf! Arf!
     //
     public static void Main(String[] args)
     {
         List<IAnimal> animals = new List<IAnimal>();
         animals.Add(new Cat("Missy"));
         animals.Add(new Cat("Mr. Bojangles"));
         animals.Add(new Dog("Lassie"));

         foreach(IAnimal animal in animals)
         {
              Console.WriteLine(animal.Name + ": " + animal.Talk());
         }    
     }
 }

And once he's got this nailed, you challenge him to define Bird (fly), and then Penguin (fly!?)

Robert Gould
Animals! The quintessential OOP example. Takes me back to some of my earliest programming classes =)
Tom
I like to add something like a "die" method to the abstract class to illustrate the differences between abstracts and interfaces
annakata
Make sure you don't use C#. That code is 90% boilerplate. Use a scripting language to get OO across, then go to a static language to explain interfaces, etc.
Jules
+2  A: 

Read the Java tutorials for some good ideas and real world examples.

adam
+1. I first got into OOP by reading the Java tutorials (with great respect and without coding a single line myself, heh). The concepts were fun to read and made it (way) easier to learn C++ later.
Tiberiu Ana
+11  A: 

The best way I got it through to my wife (a chartered accountant) is as follows.

In 'regular' programming you have data (things that are manipulated) and code (things that manipulate) and they're separate. Sometimes you get mixed up because a certain piece of code tries to manipulate the wrong thing.

In my wife's case, I said a invoice arrived (which involves no physical money changing hands) and accidentally updated a bank balance, something she immediately saw as potential fraud (she used to do forensic accounting, everything is potential fraud to her, including most of my share trades :-).

You could just as easily say that a piece of code meant to wash a floor with a huge mop decided to do it with your toothbrush.

With OO programming, the manipulators and manipulatees are inextricably entwined. You don't apply the floor washing process to the floor, instead you command the floor to wash itself. It knows how to do this because the code is part of the object, not something external to it.

In the accounting case above, I think we ended up having the chart of accounts as the object and we told it to apply a invoice to itself. Since it understood the process, it knew which accounts were allowed to be updated (creditors liability account and an expense account if I remember correctly).

Anyway, that's irrelevant and I'm just meandering now. What I'm saying is to express it in terms your target audience will understand. I suppose that's the secret of most teaching.

paxdiablo
+1, Half the challenge is understanding the student. This is why 1:1 is more effective than classes, because you can learn the students thoughts and augment your data to suit. Many however, lack patience, like myself ;)
Kent Fredric
This is definitely true. People learn much better if they can build off of previous knowledge.
Jay Conrod
+4  A: 

I second the 'Animal' approach!

This little article on JavaRanch, "How my Dog learned Polymorphism" helped me a lot (it's pretty much language independent):

http://www.javaranch.com/campfire/StoryPoly.jsp

Ruben Steins
A: 

Object-oriented programming is one technique of raising the level of abstraction by means of which the programmer communicates with the computer: from the level of flipping individual bits on and off, from the level of punching holes in paper cards, from the level of extraordinarily complex sequences of basic instruction codes, from the level of less complicated definitions of reusable templates for blocks of data and reusable blocks of code (structs and procedures), to the level of transcribing the concepts in the programmer's mind into code, so that what goes on inside the computer comes to resemble, for the programmer, what goes on outside the computer in the world of physical objects, intangible assets, and cause-and-effect.

Justice
+3  A: 

While you are explaining OO with animals, do not forget to illustrate the "is-a" relationship with Stinger missiles-armed kangaroos ;-)

The kangaroos scattered, as predicted, and the Americans nodded appreciatively . . . and then did a double-take as the kangaroos reappeared from behind a hill and launched a barrage of stinger missiles at the hapless helicopter. (Apparently the programmers had forgotten the remove "that" part of the infantry coding).

The lesson? Objects are defined with certain attributes, and any new object defined in terms of the old one inherits all the attributes. The embarrassed programmers had learned to be careful when reusing object-oriented code, and the Yanks left with the utmost respect for the Australian wildlife.

VonC
Hehe yeah this story was in one of my school books. Great story :)
Gunnar Steinn
+1  A: 

How about "each molding is built using a mold", or "each model is built using a template", and so "each object is built using a class" ?

Note that it works for class-oriented OOP (which is what you want), but not for prototype-oriented OOP.

As for explaining OOP to a programmer, I'd add examples illustrating:

Separating state from behavior

Most of the time, an instance describe a state, and a class describe a behavior.

Delegation

An instance delegates its behavior to its class, and the class in turn can delegate its behavior to its superclasses (or mixins or traits)

Polymorphism

If class A inherits from class B, an instance of A can be used anywhere an instance of class B can be used.

Messages & methods

A message (or generic function, or virtual function) is like a question. Most of the time, several classes can answer to this question.

A corresponding method is a possible answer to the question, that resides in a class.

When sending a message to an instance, the instance looks up for a corresponding method in its class. If found, it calls it (with the instance bound to 'self' or 'this'. Otherwise, it looks for a corresponding method in its mixins, traits, or superclasses, and calls it.

Sébastien RoccaSerra
A: 

the best book i've ever on object-oriented programming is Betrand's "Object-Oriented Software Construction" - if you really want to get the basics, there is no way around it.

Andre Steingress
A: 

I explain that procedural program is built around the "verbs" of the system, the things you want the system to do, whereas object-oriented programming is build about the "nouns," the things in the system, and what they are capable of, and that for many people this allows for a more straightforward mapping from the problem domain to software.

For the example, I use cars -- "Honda Accord" is a class, whereas the vehicle sitting in the parking lot is an object, an instance of a Honda Accord. A Honda Accord is a sedan, which is a car, which is an automobile, which is a motorized vehicle, which is a mode of transportation, etc. I cannot do anything with a car until I have a physical car to work with -- it doesn't help me that the idea of a Honda Accord exists.

It also helps for discussing interfaces and polymorphism -- the gas pedal means accelerate, regardless what the car does behind the scenes to make that happen. There are "private" parts of the car that I as user do not have access to -- I cannot directly apply an individual brake.

JohnMcG
A: 

Since the issue is to explain to a new programmer and not to a mother or a wife, I would go right straight to the point. OO is about three main concepts:

  1. Inheritance: a dog is an animal, parent-child, is-a relationship test, etc.
  2. Encapsulation: public-private (protected), information hiding, internal underlying details are not important to the users of the class, protect users from future changes in the implementation.
  3. Polymorphism: run-time binding, late binding, method that gets invoked depends on the type of the object and not the reference or pointer to the object.

Also, depending on how much the new programmer has been doing a procedural language, I would need to help him/her unlearn that the functions or procedures are no longer central.

Khnle
Or father, or brother? I keep having to explain to male non-techies...
chryss
despite popular believe, I think information hiding is not part of OOP at all! smalltalk is like python in that there's no explicit "private" fields. Also, information hiding applies very well to procedural programming.
hasen j
A: 

Games are good. There are gameobjects, from this walls, enemies and players inherit. The gameobjects should be renderable have collision-logic etc. The enemies have ai-logic while the player is keyboard controlled.

Some UI-elements are also good, there are buttons, inputboxes etc that all inherit from some baseobject that has code for managing mouse-events etc.

I don't like the animal-example because i've never seen a "real" program that has ever had to use of animals in that way. It will only make people use inheritance all over the place and you will end up with cubes inheriting from rectangles that inherit from lines (why does so many books insist on using this as example? ).

A: 

If they're old enough to have ever filled out a tax form, show them a 1040EZ and explain that an instance of a class is like a filled-out form: each blank is a member variable of the object, and the form also includes instructions for what to do with the member variables, and those instructions are the member functions of the object. A class itself is like a master copy of the form, from which you can print off an endless number of blank forms to fill out.

One thing that I would counsel to AVOID in trying to communicate the concepts of OO to new programmers is using only examples where objects (in the OO sense) represent real-world physical objects. This will actually make students more confused when they encounter objects used to represent non-physical objects (such as a color scheme, or most of the behavioral patterns in "Design Patterns") or objects used just as a useful way to store related functions and related data in the same place (think Java's java.lang.Math for an example.)

+1  A: 

Believe it or not, sports!

I've had success in teaching and mentoring by talking about the way that e.g. a play for a football team is described in terms of how the various positions (Center, Quarterback, Runningback, etc.) interact to accomplish a particular goal. In one version, the positions correspond to classes, and specific persons (Tony Romo, Johnny Unitas, etc.) are instances of the class -- individuals who exhibit the same behaviors as defined by the positions.

The second version of this metaphor is to explain that the positions may be interfaces (in the Java sense) rather than classes. An interface really represents a role fulfilled by any object that implements the methods of the interface. And it's perfectly reasonable for an object (via its class, in Java) to implement multiple interfaces, just as it is possible for a talented individual to play more than one position on a sports team.

Finally, the play is like a pattern, in that it describes how a set of roles interact to accomplish some specific goal.

joel.neely
A: 

OOP is a higher level of abstraction, a programmer can't really come to grasp it unless he has a good understanding of the normal (read: procedural) way of programming, and he must be able to write some programs that do something useful.

For me it took a series of several lectures by one of my university profs, where he discussed many theoretical aspects of programming, he tried to convince us that programming is about manipulating data, and that this data is a representation of the "state(s)" of the program, and some other abstract stuff that I forgot now! But the point is, it's hard to understand OOP without some theoretical abstract discussion first, and this discussion wouldn't make any sense to a person who hadn't had experience writing some real code.

After the theoretical discussion, you give an example of a moderately complex program, written in procedural style, and slowly convert it, step by step, into object oriented style. After the concrete example, you should go back to the theoretical discussion and just summarize the main points, directly relate the theoretical constructs to the concrete example, e.g you can talk about how the name, age, and salary of an employee represent his state.

hasen j
A: 

An object is a black box, which you can't see through. Public methods are buttons on them. Protected methods are buttons hidden on the bottom, private methods are dip switches inside.

Let's see a washer as an object. We don't know how it works. We don't care if it's powered by natural gas, diesel, electricity, or plutonium. However, the mechanism and internal structure will vary greatly depending on the energy source like a combustion engine is needed for some. We don't care as long as if we push a "Wash" button, it washes our clothes.

Let's turn the washer not Object-oriented. Expose all the buttons by arranging them on the top. Customers can now turbo-charge the engine by tweaking some dip switches. Make the chassis transparent. Now, you can see your energy-saving washing machine is actually hybrid-powered. There are some monkeys in it. You free them into the wild, and the machine eats up your utility bill like a gas-guzzler.

yogman
+2  A: 

I assume the target knows how to use graphical user interfaces. I found the best way is to describe OOP with stuff that they are really used for. Say

Class

A Window is a class. It has methods like

  • Show a window
  • Enable a window
  • Set the window's title

A Window has attributes. That is data associated with it. It is encapsulated into the class, together with the functions that operate on them

  • A Window has dimensions. Width and height.
  • A Window has possibly a parent window, and possibly children.
  • A Window has a title

Object

There are many windows. Each particular window is an object of the class Window. A Parent window containing 10 windows makes 11 Window objects.

Deriveration

A Button is a Window. It has dimensions has a parent window and has a title, the label of a button. It's a special kind of a window. When you ask for a window object, someone can give you a Button. A Button can add functions and data that are specific for a button:

  • A Button has a state. It can be in a pressed state, and unpressed state.
  • A Button can be the default button in a Window.
Johannes Schaub - litb
I think you'd lose some people on "a Button is a Window" - how, why, would they ask
Axarydax
+3  A: 

Like all old farts, I'd like to answer this with a story from my own life.

I started programming basic on a VIC-20. Not knowing anything else, I though this was how all computers were programmed. I thought it was a bit hard to keep track of which variable names I had used and which were still free, (scope problem). I also thought it was hard to divide my program into repeatable chunks using gosub-return and setting and reading the variables that these would use, (lack of methods).

Then I got into Turbo C over MS-DOS. Now I could create my own methods and functions! I was no longer stuck with the old finite set of commands in basic. I felt like I was creating a new language for every program I wrote. C gave me more expressive power.

C++ was the first object oriented language I heard about. The big moment for me was when I understood that I could create my own data types, and even overload the operators. Again, it felt like I could create my own language containing both new functions and data types, complete with operators.

That's how I would sell OO to a new programmer. Explain that it gives expressive power because they can define their own data types. I always though encapsulation was a better selling point than inheritance.

Guge