Possible Duplicate:
Difference between object and instance
I have couple of questions:
- Every instance of a class (except an abstract class) is an object?
- Abstract classes cannot be instantiated, hence they are not objects?
Could anyone help me better understand the above concepts as they relate to C#?
There is one common analogy to maybe clearer show what is the difference between the concepts of class and object.
A class is like a recipe. An object is a cake. From one recipe you can build many cakes. A recipe can only contain hints (be abstract) and leave room for your own creation of a recipe (implementing class) from which you can then build cakes (objects).
So a class is not an object, it's the description of what objects of that class look like. Abstract classes contain methods and other information useful for the implementation of classes deriving from this abstract class. Objects cannot be created/instantiated from an abstract class (because it's definition is not complete).
class Cat {} // It is a cat. Just a cat. Class is a general issue.
myCat = new Cat("red", "5kg", "likes milk", "3 years old"); // It is my cat. It is an object. It is really a cat.
yourCat = new Cat("gary", "3kg", "likes a meal", "5 years old"); // It is your cat. Another cat. Not my cat. It is really a cat too. It is an object;
abstract class Animal {} // Abstract class
animal = new Animal(); // It is not correct. What is 'animal'? Cat, dog, cow? I don't know.
class Dog : Animal {} // It is a class. It is a dog in general.
someDog = new Dog("brown", "10 kg", "likes cats"); // It is really a dog. It is an object.
An abstract class can never be instantiated (and so can never become an object). If you create class that inherits from an abstract base class, and instantiate it, it will have properties of the abstract class as well as its own properties. The objective behind creating an abstract base class is to "enforce" deriving classes to implement certain functionality (similar to an interface, but not quite).
Every instance of a class (except an abstract class) is an object?
Yes. That's the definition of "instance.
Abstract classes cannot be instantiated, hence they are not objects?
Classes are not the same as instances or objects. An object is an instance of a class. Imagine a zoo simulation. An object is like a specific lion, whereas a class is more like a description of what a lion is. Abstract classes are descriptions of things that are too vague to make it reasonable to build something based on that description alone (e.g. "Animal"). That's why you can't instantiate them. Something like an Animal might be too vague for the program to request directly; instead, the program will only request more specific things like Lion, Tiger, or Mongoose.