tags:

views:

449

answers:

13

Can we say that a Class is a Object?

+11  A: 

An object is an instance of a class.

William Brendel
And not just in C++ either, in most if not all OO languages.
AlbertoPL
Class is an object in Java.
CookieOfFortune
True, but doesn't explain anything. Not really helpful to the OP.
jalf
@jalf: To be honest, I thought this question would be closed long before this answer got any up votes. I answered almost as a joke, but for some reason people up voted it. Strange world we live in...
William Brendel
@CookieOfFortune: isn't Class actually a class in Java? Instances of Class are objects representing classes (types), and there can be a Class that means the Class class... but I'm pretty sure saying Class is an Object is false.
rmeador
@rmeador: Yes, Class is a class in Java. In fact, it's a generic, so when you load a class called Foo from an instance of ClassLoader, what you get back is an instance of Class<Foo>.
William Brendel
@AlbertoPL: In most OO languages, not all of them. Some of them have no distinction between class and object: you just duplicate or inherit from what are essentially objects. There may be other ways of doing OO that I haven't run into yet.
David Thornley
+3  A: 

No, an object is an instance of a class.

stevedbrown
You wasted 5 seconds by typing "No, " at the beginning of your sentence! (:
Alex Barrett
Hah hah, i know.
stevedbrown
I have to say, by adding "no" at the beginning, stevedbrown did technically answer the question more accurately than I. Can't believe this is a real question though :-)
William Brendel
This is apparently a common point of confusion for beginners.
TokenMacGuy
@TokenMacGuy: Oh I didn't mean to imply this topic isn't confusing for beginners. I guess I am just used to a time when if you knew enough to ask a question on SO, you knew the basics of OOP. I'm actually glad this type of question can be asked without being closed immediately. I think it shows that the community is expanding from a relatively small subset of "hardcore" programmers.
William Brendel
The word no means a difference of several votes.
CodeFusionMobile
+27  A: 

A Class is like a blueprint, an object is like a house built from that blueprint.

You can have many houses with the same layout/floorplan (read class), but each is it's own instance (read object). Each has it's own owner, furniture, etc.

CodeFusionMobile
I love how the only question which actually *explains* anything is the only one that hasn't been upvoted... +1 from here then. :)
jalf
+1 Nice analogy. I think it could even work when you factor in things like inheritance and polymorphism.
William Brendel
Oh definately. You could think of inheritance as adding an addition to the home, or adding a garage or something of that nature.
CodeFusionMobile
My faith in humanity is restored. Your thoughtful, helpful answer has beat my short, flippant answer. I still can't believe mine got as many up votes as it did :-)
William Brendel
i like this answer. i was totally failing with my own one.
Johannes Schaub - litb
But, can we say that a Class is an Object? As in, a blueprint is something built from another blueprint?
Albert
@Albert, in C++, that would be a class-template, instantiating the classes, i think :) The color of the blueprint is its template parameter :p
Johannes Schaub - litb
Way to push the analogy :)
CodeFusionMobile
@Albert: No. The analogy breaks there. In C++ there are no instnaces (objects) of the blue print. The blueprints are only used in the designing and building the house. At runtime you can have no instances of the blueprint. In langauges with reflection the line becomes a bit more blured but C++ has not reflection so the answer is no.
Martin York
Thanks for the responses!
Albert
That's really a nice way of explaining it. +1 for this :)
Juri
@Jay. you an safely accept this answer already.
Steve Obbayi
A: 

This is analogous to asking if a cat is a "My Kitten Mittens".

TokenMacGuy
+3  A: 

An object is some data, which has an address in run-time memory.

There are different types of object (e.g. int, float, etc.). You can create user-defined types, called 'classes'.

For example, I can define Dog as a class ...

class Dog {};

... and then create several objects, each of which is one instance of that class ...

Dog fido;
Dog spot;
ChrisW
+1 by me. but looks like the crowd is after the "oop object thingy". our c/c++ specific notions of "object" doesn't seem to appeal the folks -.-
Johannes Schaub - litb
The OP has a "c++" tag.
ChrisW
A: 

C++ supports many paradigms, but not the 'everything is an object' style of object-oriented programming. Classes exist in the source code, but do not exist at run time. Even runtime type information doesn't preserve Classes as object, but only provides a capability to get opaque ids which correspond to the type.

Pete Kirkham
A: 

In C++, Objects are essentially the variables and Classes are the types of their values.

Nick D
A: 

Both classes and instances are objects, but object oriented programming doesn't force the language to have classes & instances.

Georg
A: 

Short Answer: In Languages C++, Java: No. In Languages like Python: Yes

In java, classes are objects and can be loaded at runtime.
Johannes Schaub - litb
@litb: it's a bit more subtle than that, though, because of Java's static typing. A Java class (or interface) is a static type, as well as being represented by an object at runtime. That type is not represented in javac by the same object as it is at runtime (how could it be - they're different JVMs). So I'm not sure it gives the full story to say that the object and the class are identical. It's much closer in Python, where a class really is just an object that happens to be capable of creating other objects and giving them a bunch of methods.
Steve Jessop
I stand corrected! Well some time ago i asked dudes in ##java about that, and they told me that classes are just objects, and you can access them with ClassName.class . But now i asked again, now some other nicks tell me the truth: Those "class objects" just represent the classes, they aren't the classes itself. The class still needs a definition at compile time, like you say. Thanks for your insight!
Johannes Schaub - litb
To be honest I wouldn't normally contradict someone who says "classes are objects in Java", any more than if they said "database rows are objects in the Hibernate ORM". Normally we allow "is" to mean "is represented by". It's just that comparing Java with Python, "classes are objects" is less true in Java. But still much more true than it is in C++, where a typeid obviously isn't the same thing as the class it represents :-)
Steve Jessop
I obviously need to get my hands dirty with python, i'm TEH python n00b out there. Just asked in #python about that classes-are-objects, but they confused me with "type is an instance of object, and object of type, and type is derived from object" and i was like "wtf??!". lol
Johannes Schaub - litb
+1  A: 

Class: A class defines a particular type's behaviours and properties.

Object: An object is an instance of a class.

For example, if you have a Dog named Bingo.

  • Dog would be the class defining its behaviours and properties

  • Bingo would be an object that is an instance of the Dog class

Strictly speaking, a Class is not an Object in C++. But in languages such as C# and Java that supports reflection, classes can be used like objects but that is a more advance topic and probaly not what the original question is asking.

Aidan
+1  A: 

I will try to give more technical explanation rather then an abstract one , I think that definitions like "a class is a blueprint and an object is something made from this blueprint" are impossible to understand for newbies simply because these kind of definitions are abstract and context less.

Classes and objects have a pure abstract meaning in the object oriented world but for simplicity I will reduce the definition to a more practical one.

consider the following statement:

int a;

"int" is a type , "a" is a variable which have the type : "int".

c++ enable various ways to let the programer define new types , for example:

typedef int* int_ptr;
int_ptr a;

In this example , a new type is defined int_ptr. "int_ptr" is a type , "a" is a variable which its type is "int_ptr". Another example:

struct Point
{   
    int x;
    int y;
};
Point a;

Here , a new type is defined , "Point" , "a" is a varaible which is type is "Point"

So what is a class in c++? a class is another way to define a new type , just like the other ways mentioned above.

What is an object? an object is a variable which has type that was defined using the class keyword.

For example:

class SmartPoint
{
public:
   Point(x,y);
   Move(x,y);
protected:
   int x,y ;
};

Point a;

In this example , a new type is defined , "SmartPoint". "a" is a variable which its type is "SmartPoint".

You may ask then what is different between a type defined by using the "class" keyword or "struct" keyword or "typedef" , well this is a matter for another discussion.

+1  A: 

No, an object is an instance of a class...

Unless...

If you are implementing a software design tool that allows you to represent classes, interfaces, properties, inheritance, associations, aggregations, etc., then at runtime, yes, each class you place in the designer will be an object instance of the Class class. Ok, couldn't help myself finding an example so twisted and meta.

Now seriously, a class is not an object.

Rui Craveiro
A: 

A class is not an object.

In simpler C language, a class is like a struct type, but more complex. Using a C struct example as analogy:

struct class_ {
    int attribute00;
    float attribute02;
    ...
}

struct class_ object_ = {0, 0.0, ...};

struct class_ is act like a class and object_ is act like an object. struct class_ has no physical storage in memory, object_ has physical storage in memory.

In human language, a word 'house' (as class) can defined in dictionary as place to stay, with doors, with windows, and rooms that you can only speak with your mouth to tell other people what is a house. A physical house (as object) is a solid build house on a land that you can move in and stay with your family.

A word 'house' take no physical occupation of land or space. A physical house occupy land and space.

ttchong
Also, a physical house can have a name "ttchong house" or "white house" or "downing street 10". A word 'house' can not have a name.
ttchong
By the way, this seem to be a very beginning question for programmer in OO. However, I found it is really fun to get my self rephrase what I understand in a more precise and easy to understand way.
ttchong