Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Dhineshkumar raj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic