Incremental Java
Classes vs. Objects

Classes

You decide to cook a dish. Perhaps you are making a tofu stir fry. You have a recipe for this. Is the recipe the same thing as the dish itself? It's not. A recipe tells you how to make a dish, but it isn't a dish.

This is the same relationship between a class and an object. A class tells you all you need to know about an object, but it isn't an object.

A class designer designs classes. From the class, you can then create (or construct) objects.

Suppose you worked at a restaurant as a chef. Several customers have heard of your famous tofu stir fry. Three of them order it. You make three dishes. Each of these dishes could be thought of as an object. The object didn't come out of nowhere. You had a recipe you were following, and used that to make each dish.

The recipe might call for tofu, red peppers, baby corn, mushrooms, and water chestnuts. Thus, each dish has these ingredients based on the proportion in the recipe.

While Java objects aren't dishes, they are analagous. Java classes aren't recipes, but they are analagous.

Instances

In Java terminology, we say that each dish is an instance of the tofu stir-fry class. Instance is another word for object, although it suggests a closer relation to class.

There are languages that support objects that don't necessarily support classes. Classes are like cookie cutters. They allow you to make many cookies that look alike.

You could also make objects where one object has significant differences from another object, and there's no mention of classes.

Classes are Types

You know that String is a type. It is also a class. Object types are classes. Classes not only tell you how to create an object, but they are a type. Thus, you can create a Foo class, and in the process, you have a Foo type and can declare Foo variables.