Tuesday, December 9, 2008

What is the difference between an Object and a Class in Java?

Welcome back to java code online. This is one of the basic question of Java. In Java everything is an Object, but a Class is used to make it. The exact definition is that "A class is a blueprint for an object". Particularly it tells the JVM about how to make the Object.

When I say that a class is a blueprint of an Object, I mean that every Object in Java made from that class is practically the same but can have its own specific values. Like whenever a building is made, then a blueprint is made beforehand. The blueprint defines the structure of the building, but using the same blueprint you may create many separate buildings. Each building can have its own color on the walls, its own particular size, etc. But the overall structure will be the same.

I will take one more example to clarify my point. We know that the process of making a car is totally automated. Now the cavity which molds the shape of the car is the same, but the same cavity is used to mold several cars. Like in the Mercedes factory, each Mercedes coming out from the car molding cavity will be practically the same. Though each Mercedes can later be given its particular color, alloy wheels, extra bumper, advanced music system, etc. Here the cavity which molds the car is the "class" in Java, and the Mercedes car coming out from that cavity is the "Object" in Java.

Now you would be easily able to visualize the difference between a class and an Object in Java. I hope that this article was beneficial in helping you distinguish between an Object and a Class. If you liked the article then do leave a comment. I would love to hear from you. For more info on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.