wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Very beginner question, still confused about classes vs object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very beginner question, still confused about classes vs object" Watch "Very beginner question, still confused about classes vs object" New topic
Author

Very beginner question, still confused about classes vs object

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Ok so Java is my first OO language (and to be honest the first language I have actually spent significant intimate time with), so I am still confused about the different basics items(things, objects, whatever) in it. I looked up object on Wikipedia (no teacher, i am not referencing wikipedia) and here is the first line:
In the programming paradigm of object-oriented programming, an object is an individual unit of run-time data storage that is used as the basic building block of programs.
To me, that sounds like a class. Does someone have a clear way to understand objects vs classes? And maybe just for kicks, methods and instance variables too seeing as Head First Java seems to feel I should (and probably with good reason) have a good grasp of these by now.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Hi Jake,

I know that feeling!

The way I think of it, a class is sort of a template for an object. The analogy breaks down since 'static' members belong to the class as a whole (one time only) rather than living in each of the objects. For some strange reason I think of objects as bulky, and classes as sleek and thin.

Right. Reading what I wrote convinced me that I have just made things worse
[ July 18, 2007: Message edited by: Katrina Owen ]
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Haha, well thanks a lot for the response! I will reread it in the morning when my mind is fresh and not completely Java-overloaded and see if it makes more sense. I sort of get it... maybe if I keep going I will get more examples. I tend to learn best with actual examples.

Just an FYI, the link in your sig doesn't work.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
If you search around, you will probably find hundreds of different analogies about classes and objects and the like. The instructor in my first Java course used an analogy of a car to drive the concept home to a bunch of glassy eyed freshmen who had no idea what he was talking about. But that analogy has stuck with me.

If you think of a Car in a general sense, i.e. without thinking about any specific make or model of Car, you can make some general statements. I'll take this opportunity to make three such statements:

A car needs fuel to run.
A car has an accelerator (gas pedal) that will cause the car to move faster if its pressed.
A car has a brake that will cause the car to move slower and eventually stop if depressed.

We can formalize these ideas in Java by creating a class that captures these essential features.


This construct can be read as saying, we are describing a class of objects called Car, every real object in this class will be able to do these basic things. This is a very high-level abstraction of a car that omits many details, but thats fine for now.

Now when we thing of a particular instance of a Car, or a Car object, we already have some frame of reference for it. If someone hands us a Car object, no matter where it came from, we can assume that somewhere it has a port for us to add fuel, and an accelerator for us to make it go faster, and a brake for it to stop.

However individual Car objects also have their own state. For instance right now *my* Car object has about a quarter tank of gas. It has an accelerator, but if you press it it may hesitate for a second or two before slowly (imperceptibly perhaps) beginning to speed up. The brakes will definitely cause it to stop, and as a side-effect you might be treated to a high-pitched squeal, and a fair bit a smoke for the effort.

So you can have a class of objects -> Car

And an object that belongs to that class that carries its own state -> my crappy car with a quarter tank of gas, bad accelerator, and squeaky brakes.

Does that help out at all?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Shrikanth Nekkanti
Ranch Hand

Joined: Jul 12, 2007
Posts: 48
Mr Jake Miller

I understand your problem.
Let me give you clear definations of class and object.

Class is an entity that does not exist physically.
class is a collection of objects belonging to same category and exhibit same behaviour.

Let us come to objects

object is an entity that exists physically
object is an instance of class

Now come to examples.

If i ask you to bring sweet you will bring me either jamoon or pastry.

so jamoon , pastry are objects of sweet class which does not exist physically it is just an idea for creating objects.

send me your feedback @
shrikanth.nekkanti@tcs.com
without fail.
If you need i'll give more information.
Shrikanth Nekkanti
Ranch Hand

Joined: Jul 12, 2007
Posts: 48
Mr Jake Miller

I understand your problem.
Let me give you clear definations of class and object.

Class is an entity that does not exist physically.
class is a collection of objects belonging to same category and exhibit same behaviour.

Let us come to objects

object is an entity that exists physically
object is an instance of class

Now come to examples.

If i ask you to bring sweet you will bring me either jamoon or pastry.

so jamoon , pastry are objects of sweet class which does not exist physically it is just an idea for creating objects.

send me your feedback @
shrikanth.nekkanti@tcs.com
without fail.
If you need i'll give more information.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
A class is like blueprints
An object is like a house

blueprints describe how to build a house

If I want that house BUT I want two extra rooms added on then we take the original blueprints, draw the new rooms in their positions. We have now extended blueprints "House" and created blueprints for "House2".

If I want to take an original class and add two more ints, I edit the original, add the two new ints, and save it as a new class.

I can use either set of blueprints to build a house,

I can use either class to instantiate an object

If you want to use the US Mail services you may want to implement a Mailbox. The type of box is prescribed by the US Postal Service but they are not going to tell you how to walk from your house to the mailbox to get your mail or drop off your outgoing mail, you have to do that part on your own. But the box is defined and the set of services is defined (they will drop off your mail, and pick up your mail to take with them) but it does not tell you how to do those things, you have to implement those activities yourself. This is sort of like "implementing an interface". The mailbox does not really change the house, per se, it simply adds an interface to the US Mail.

I hope you get a laugh out of this...... but it isnt that far from the concept....


------------------------
Bob
SCJP - 86% - June 11, 2009
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Wow, this forum is beyond helpful. Thanks all! I am starting to grasp it a little better. I like the class -> blueprint, object -> actual instance of blueprint idea. I guess now thinking about it, the words "class" and "object" sort of make a little more sense. An object is a specific object for a general class. Thanks again!
 
jQuery in Action, 2nd edition
 
subject: Very beginner question, still confused about classes vs object
 
Similar Threads
Delphi vs. Java
Java vs C++
Wide & Shallow VS. Narrow & Deep
code vs. design
Testing Active Vs Passive or Explicit vs Implicit