This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what is the exact definition for Class and Object. with example   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the exact definition for Class and Object. with example   " Watch "what is the exact definition for Class and Object. with example   " New topic

what is the exact definition for Class and Object. with example

Dhineshkumar raj

Joined: Aug 06, 2007
Posts: 28
What definition we can give for Object??

Can any one tell me the real example for Class and Object.
but please dont give example like this..
( class as a vehicle. An object as a lorry or car or rocket.
A lorry is an object, so is a car and a rocket too. But all of them are vehicles.)
Its seems the example for inheritance.
I think its like parent/child relationship.
Like super class is Animal. Say subclass Elephant, Bear and Tiger are example for objects.
subclass Tiger has its two types of behavior,
1) Own behavior - like (kills others animals and eats)
2) Common behavior - like (All animals don't have 6th sense and lives in forest and so on.. )

please any one give me the proper definition for Class & object with examples.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37945
Disagree about all. And it is class, not "Class", object not "Object."

What colour is a car? What speed is it travelling at? "Car" is a class. Cars can have colours, numberplates, can travel at different speeds.
Aminals may be eating at the moment, or sleeping at the moment. Is a lion eating or sleeping just now?

"Car" is a subclass of "Vehicle" and "Lion" is a subclass of "Animal".
A class records that an animal might eat or sleep, that a car might travel at 20mph or 100mph, but it doesn't record that "Lion" is eating or hunting, nor that "Lion" is asleep or awake. It doesn't record a speed or colour for "Car," either.

You can create an object from the Car class and record that it is coloured black, that it is travelling at 50mph.
You can create three objects from the Lion class and record that one is eating, one asleep and the third is hunting.

A class is an abstract definition, an object is a concrete implementation.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11153

In a class file, you define the parts of the thing.

"my class will store THESE pieces of information, and let you do THESE things with it".

an object is a SPECIFIC INSTANCE of said class. myObject stores 'blue', '100', 'stick', and 'orange'.

put another way, the class file is the blueprint. an object is a house. you can use the same blueprint to build hundreds or thousands of houses that all behave the same (lock front door, turn on living room chandelier, fill bathtub), but have different color bedrooms, some have siding and some are brick, etc.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Campbell Ritchie:

"Car" is a subclass of "Vehicle" and "Lion" is a subclass of "Animal".

That really depends on the kind of system you build. It's not too hard to imagine a system where Lion would in fact be an instance of the Animal class, for example a reference work on animals.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Here is my understanding of those terms:

An object is an entity that has both state and behavior.

A class is a template for objects. It is a common definition of both the behavior and the possible states the objects posses that are created using that template (the instances of the class).

Note that there are languages (for example Smalltalk), where classes again are objects (with their templates being meta-classes). And there are languages where you can create objects without having classes, for example prototyping based languages (like JavaScript).
subject: what is the exact definition for Class and Object. with example
Similar Threads
what is the example for polymorphism?
Why a class declared as Abstract, cannot have any abstract method declaration?
Diff types of associations
My dog is smarter than I in regards to polymorphism
Abstracf Vs Interfaces