views:

498

answers:

19

My neighbor is taking "Intro to Java", and asked me to help explain a few of the first-day concepts. I realized that since I do this everyday, I don't have the beginner's mind, and it's hard to relate some of this stuff from scratch.

The one that's actually not trivial for me to explain is "what the heck is a class?"


Best I have so far:

  • A variable holds some kind of data; one variable might be a first name, another variable might be your weight in pounds.

  • A method is a function, it does stuff, and can do stuff with those variables. A method might display your name on screen, or tell you how much weight you should lose to have a good BMI ratio.

  • An object holds both variables and methods; one object might represent you, a second object might represent me.

  • A class is kind of the blueprint or template that describes the methods and variables that will be in each object. An object is an instantiated (instance of a) class; an object is something, while the class is simply the plans to make that something.

Continuing the example, we have a Person object, which is instantiated to hold Alice's data, and another Person object instantiated to hold Bob's data, and another for Carol, and so on.


How do I tune this example to make more sense, and/or what's a better approach? The word "instantiated" feels too heavy at this point.

(I think this is a useful question, but is obviously subjective; marked as community wiki.)

+2  A: 

class == cookie cutter, object == cookie.

duffymo
No, that would be a factory.
T.J. Crowder
Nope, I disagree. The class specifies methods and attributes for all its instances.
duffymo
class = cookie maker. But the class=blueprint is still better. A building blueprint stands for a class as the building stands for an object.
Tom Brito
@duffymo, you beat me to it. This explanation works well for CS/OO civilians.
Kelly French
I think a better analogy is class == recipe, object == cookie.
ataylor
Cookies are tastier. I think they resonate better with non-programmers. 8) Recipe might work, but I was trying to be pithy and fast.
duffymo
class : cake recipe :: object : chocolate cake warm from the oven
Kelly French
Mmmmmmmmmmmmmmmm - chocolate cake warm from the oven.
duffymo
+19  A: 

Class : Object :: Blueprint : Building

Paul Sasik
you mean `Class : Object :: Blueprint : Building`
Inverse
Good call, fixed. i originally had a wordy sentence for the explanation. I noticed that the answer could be shortened to an SAT/ACT-style is-to/as/is-to (what's the proper name for that sort of comparison?) but when i replaced the words with : and :: it became logically inconsistent.
Paul Sasik
+9  A: 

"Car" is a class. My car, sitting in my driveway, is an instance (object).

T.J. Crowder
A: 

Panda DNA is a class. A Panda running around, eating and performing Panda-like activities is an object.

karim79
cool, but hope he's good at biology.. XD
Tom Brito
That analogy doesn't really make a lot of sense. A class is not just just the code of its constructor, which is the closest thing DNA maps to.
munificent
+1  A: 

If and only if he is familiar with Plato's Theory of Forms, you can make an analogy where classes are like Plato's forms and objects are like Plato's real world objects.

See this post for a full description.

epotter
+1  A: 

One of the examples I use during my java courses is the Human class.

Everyone reading this is a Human (I least I hope so !), we all have our differences our resemblances but at the end we're all Human (After all).

Each Human (known as an instance or object) has specific characteristics such as the eyes color or the voice which are the fields (you called that variables, but the right name would be fields). But the values are different from an Human instance to another.

There is also a common knowledge, shared with the humanity, principles like the "Pythagorean theorem". This knowledge is common, it can be interpreted as a static field (I know it's an exaggeration) which means that this knowledge is not only contained in one human but in the humanity.

Every Human can do things such as walking, speaking etc. this is known as method, walking is the same for everyone, but when I walk, not everyone walk. The act of walking only affects the Human instance which does this, but still it's defined by the Human class


If you want to get deeper in OOP, Teaching OOP to non-programmers

Colin Hebert
+2  A: 

class:: Man or Woman

object:: me, you ...

mohammad shamsi
A: 

If they are learning to program OO have them use BlueJ. They should get the differences after walking through the first tutorial.

You define the classes and when you instantiate them they actually appear at the bottom of the GUI at which point you can call methods on them.

It really helps get the point through better than any analogy you want to try. Even if you nail the analogy, it doesn't translate into code for someone who hasn't learned OO yet (even though for all of us it seems really natural and all these descriptions make great sense.)

Bill K
+1  A: 

Class: Girl

Object : that girl, this girl, my girl...umm maybe not.

Yea all girls should have the properties of a Girl (class in this case).

loxxy
girls are not objects.. you should treat them better..
Tom Brito
I had actually started by writing BOY .... appeared a bit gay :p
loxxy
Not that there's anything wrong with that....
duffymo
+2  A: 

An object is a thing. A class is a category of things.

"Person" is a class; you are an object, an instance of the Person class. Also, the word "you" can be thought of as a variable, since it refers to a Person, but not always the same Person.

Mike Baranczak
A: 

OOP is just one more way of representing Abstract Data Structures in programs. In object-oriented terminology, the type is called a class, and the variable with that type is called an object. More on type <-> class, variable <-> object correspondence.

kemiisto
True, but irrelevant. We're talking about explaining these concepts to an absolute beginner, not to someone who's moving from C to Java.
Mike Baranczak
Explaining OOP to an absolute beginner is nonsense anyway. Everything is good in its season.
kemiisto
And by the way do you really think that all this cookies/cars/blueprints/whatever examples really explains something? "Surely You're Joking, Mr. Feynman!" =)
kemiisto
+1 for invoking Feynman's name.
duffymo
A: 

You might find this talk by Guy L. Steele interesting: http://video.google.com/videoplay?docid=-8860158196198824415#

Thorbjørn Ravn Andersen
+2  A: 

Object Oriented programming is about creating programs using as building blocks, "things" that exists in the real world, these real world things are called objects, hence object oriented

For instance, if you're creating a Address Book program, you may define the following objects:

person, address, phone

Among many, many others. Those would be real life objects, and you describe your program in terms of these abstractions.

With that in mind you can start describing some concepts.

Class is used to define the characteristics an objects will have. A class is used only as a template, or a blueprint. For instance, for all the persons in your address book, you may say they all will have:

Person:
   - name 
   - last name 
   - phone number 
   - address 

Etc.

An address may have:

 Address:
    - street 
    - number
    - city 
    - zip code 
    - country 

And so on. As you can notice, a class me be defined in terms of other classes, for instance, in this context, a person has one address.

An Object is a particular instance of a given class. When you add an entry to your address book, you create an object and fill in the attributes.

 onePerson  ofType Person is (  
     - name = "Oscar"
     - last name = "Reyes" 
     - phone number = "56 58 11 11"
     - address = anAddress ofType Address (
                     - street = "Tecolotes" 
                     - number = 32
                     - city   = "D.F." 
                     - zip code = 23423
                     - country = "Mexico"
                 ) 
  )

So, this object a class instance with data. Other entry in the address book are other objects with different data.

That shows the difference between them.

There are other relevant concepts in OOP that are worth listing, and interrelate with the concept of object and class:

Abstraction You don't need to list all the attributes of a person, to use it. for instance, in this case, you don't care if that person is single or married, even when in real life, persons are either single or married.

Encapsulation Attributes from the person are hidden to other objects and are accessed through methods, this prevent from data corruption.

Polymorphism A different type may respond differently to the same message or method.

Inheritance classes may have subclasses and attributes and behavior which inherit the characteristics of the super classes.

OscarRyz
+3  A: 

A class description is like a blueprint for a house. All the houses built from that blueprint are objects of that class. A given house is an instance. A tenant can be a changing variable in the house. An example of a method is the procedure by which the post office sends and receives messages (mail) to the house via its mailbox.

joe snyder
A: 

I always define them as blueprint and product.

A blueprint describes the complete product in every detail, the product is the result that comes out of the machine.

baklap
A: 

Object is an instance of a class Variable is an instance of a type

That given,a class can be something like "type on steroids": it can have : variables which can be from any type or objects from another class methods,which can operate on class variables in the same way as different types have their methods(for example +(bool,bool)) can have access to the class variables and it's all defined by yourself!

You can use the classes to model a problem in the optimal way. But there are sometimes other ways to do it ;)(not only OOP)

Alexander Ivanov
+1  A: 

If your neighbor is into classical philosophy, classes are Plato's Forms and objects are the things we see everyday that are based on the Forms.

Jeff
A: 

I would highly recommend telling him to buy a copy of a book called The Object-Oriented Thought Process by Matt Weisfeld. Its a really good conceptual introduction to OO. I've lent out my copy to a few people who were just getting into OO and they really liked it.

Javid Jamae
+8  A: 

A class and some class instances:

Courtesy of wikipedia

(public domain image hosted by wikipedia)

McDowell
I like this! Fantastic explanation!
Alex
Okay, this is one that's going to the neighbor.
Dean J