What is the difference between classes and object instances?

45,113

Solution 1

If channel is a class, Start Sports, BBC, and ESPN are its objects. If water is a Class, "Fresh Lime Water" and "Soup" are its objects.

Although you might find this explanation vague, this is the answer that I could think of.

Here is how you can learn about and distinguish classes:

Say you have a class "CAR"

Its objects are Hyundai, Ford, Suzuki. It will have the same methods but different designs -> this is how you can relate objects and classes with the real world.

Solution 2

A class specifies the behavior of its instances.

A class is an instance of a class too (class for a class is named a "metaclass").

A class is an abstraction. You find a class by finding generic properties applying to a group of objects.

Then a class is a template which defines the methods (behavior) and variables (state) to be included in a particular kind of object

Recognition of classes (outside classroom) requires experience.

Read anything from Alan Kay, he is the inventor of Object Technology, and one of the inventors of Smalltalk, the only pure objects environment as of today.

Solution 3

I do not have much programming experience, but a friend of mine made a good example of defining a class and a object. I'll try my best to use human language as possible.

Take an horse. What makes you know that this animal is an horse and not a... dog? Because it has four legs, it's a big animal, it's very strong and it can run. Well, you just defined a 'horse' class in your head! Now, you are going to see a white female horse called 'Pollyanna' and a black male horse called 'Demon'. As soon you see them you know that they are horses, so they belong to the 'horse' class. What makes them different? The sex and the color... Those are properties of the 'horse' class. 'Pollyanna' and 'Demon' are two objects, they are the real thing, things you can actually touch and ride. Their properties, sex and color are also different. Pollyanna is white and female. Demon is black and male. Those defined properties are what distinguishes one object from the other. One horse from the other. But they still belong to the same class: they are always horses!

More technical now... A class is a more abstract definition of something. A blueprint. An object is the real thing that belongs to that class. When you create a new object from a class you are instantiating an object (aka creating an instance).

Hope this helps, and if it doesn't sorry... As I said before, I do not have much programming experience :)

Gianluca

Solution 4

I'll give you a classic explanation, you can find different versions of this all over the place.

A class is like a blueprint. Say you want to build a car, the first thing you would need is a plan, This is the class. The plan will describe 'methods' such as brake and hoot. It will also describe the various components of the car. These are variables.

A car object is an instantiation of a car class. You can have lots of these for one car class.

For example:

class Car:
    def __init__(self,color):
        self.color = color

    def hoot(self):
        "do stuff"


red_car = Car('red')
red_car.hoot()
blue_car = Car('blue')
blue_car.hoot()

Now, depending on the language you are using classes themselves can be objects (this is the case in Python). Think of it this way: All blueprints have some stuff in common. That common stuff is described in the blueprint's class (which is in itself a blueprint).

Then on the point of 'water' as a class you can approach it in a few ways depending on what you want to do:

way 1:

rather have a class called Liquid with variables describing stuff like viscosity, smell, density, volume, etc. Water would be an instance of this. So would orange juice

way 2:

say you were putting together a game with a bunch of blocks that would be made up of different terrain. You could then have classes such as Grass, Water, Rock, etc (think Minecraft). Then you can have a water class instance (a water object) occupy a specific position on the map.

Share:
45,113
swaminathan_manickam
Author by

swaminathan_manickam

Updated on July 09, 2022

Comments

  • swaminathan_manickam
    swaminathan_manickam almost 2 years

    A class is a binding of methods and variables in a single unit.
    An object is an instance of a class.

    These are definitions for classes and objects in programming books. My friend said that a class is a blueprint of an object. An object is a real "thing" of a blueprint. He has given an example: if Company is a class, INFOSYS, CTS, and TCS etc are objects. Whenever I think about classes and objects, these definitions confuse me. If channel is a class, what would be objects for the class? If water is a class, what would be objects of class? Can you clarify?

  • swaminathan_manickam
    swaminathan_manickam over 11 years
    thank you for your clarification.If Channel is a object what would be a class?
  • droidchef
    droidchef over 11 years
    I already mentioned it in my Answer Please Read it. If CHannel is the object ESPN, START sPORTS, TEN SPORTS, BBC, CNBC etc. are its objects
  • Xypron
    Xypron over 3 years
    This does not explain the difference between object and instance. Cf. stackoverflow.com/questions/2885385/…