Well, that’s not what classes are all about. Classes are serious stuff. What’s more, classes are useful. Many reputable studies have shown that classes and object-oriented programming save time and money.
Even so, the notion of a class can be elusive. Even experienced programmers — the ones who are new to object-oriented programming — have trouble understanding how an object differs from a class.
Classes, objects, and tables
Because classes can be mysterious, I’ll expand your understanding with another analogy. The image below has a table of three purchases. The table’s title consists of one word (the word Purchase), and the table has three column headings: the words unitPrice,quantity, and taxable. Well, this has the same stuff —Purchase
, unitPrice
, quantity
, and taxable
.class Purchase {
double unitPrice;
int quantity;
boolean taxable;
}
So in the image below, think of the top part of the table (the title and column headings) as a class. Like the code above, this top part of the table tells you what it means to be a Purchase
. (It means having an unitPrice
value, a quantity
value, and a taxable
value.)
A class is like the top part of a table. And what about an object? Well, an object is like a row of a table. For example, with this code, you create two objects (two instances of the Purchase
class).
class ProcessPurchases {
public static void main(String[] args) {
<strong>Purchase purchase1 = new Purchase();</strong>
purchase1.unitPrice = 20.00;
purchase1.quantity = 3;
purchase1.taxable = true;
<strong>Purchase purchase2 = new Purchase();</strong>
purchase2.unitPrice = 20.00;
purchase2.quantity = 3;
purchase2.taxable = false;
double purchase1Total = purchase1.unitPrice * purchase1.quantity;
if (purchase1.taxable) {
purchase1Total *= 1.05;
}
double purchase2Total = purchase2.unitPrice * purchase2.quantity;
if (purchase2.taxable) {
purchase2Total *= 1.05;
}
if (purchase1Total == purchase2Total) {
System.out.println("No difference");
} else {
System.out.println("These purchases have different totals.");
}
}
}
The first object has unitPrice
value 20.00
, quantity
value 3
, and taxable
value true
. In the corresponding table, the first row has these three values — 20.00
, 3
, and true
.
Some questions and answers
Here’s the world’s briefest object-oriented programming FAQ:- Can I have an object without having a class?
No, you can’t. In Java, every object is an instance of a class.
- Can I have a class without having an object?
Yes, you can. In fact, many Java programs create a class without an object. That’s just fine. It’s business as usual.
- After I’ve created a class and its instances, can I add more instances to the class?
Yes, you can. In a for loop, you could create a dozen instances and you’d have a dozen rows in the table. With no objects, three objects, four objects, or more objects, you still have the same old class.
- Can an object come from more than one class?
Bite your tongue! Maybe other object-oriented languages allow this nasty class cross-breeding, but in Java, it’s strictly forbidden. That’s one thing that distinguishes Java from some of the languages that preceded it: Java is cleaner, more uniform, and easier to understand.