Tuesday, 6 December 2016

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

This article is solely for all beginner programmers, who are learning object oriented programming language e.g. Java, C++ or C# and aspire to do well on any programming interview. The difference between class and object is one of the most common questions, you would like to ask a fresher coming out from college or training institute, but you would be surprised how many beginner Java programmers struggle with this question. Class and Object are two pillars of Object Oriented Programming (OOPS) and a good understanding is a must, but when you ask this question apart from the theoretical and bookish answer that "class is a blueprint and objects are actual things created out of that blueprint", you would hardly get anything substantial. Though that answer is correct and works perfectly, it doesn't differentiate between a programmer, who has just mugged the answer, or the one who truly understand the difference between class and object.

So, if I receive that answer, I usually ask them to create a class and explain how objects are created in the program, let's say to represent an Employee, Student or simply a Car. If programmer truly understand what is a class and what is an object, it will do that in no time, but if he has just mugged up the answer, then he will be totally confused.

Point is to understand the difference between class and object, not to mug up for an interview, Why? because if you know your class and object, it would be lot easy for you to work with an object oriented programming language like Java or C#.

To give you some more example of class and object, if Car is a class than Merc, Audi and BMW are objects. If Television is class than Sony Bravia, Samsung Smart tv are its object.

If Smartphone is a class then  iPhone, Samsung Galaxy and Nokia Lumia are their object. In an object oriented application, generally nouns are represented using class, for example in finance domain Order, Trade, Instruments are classes.

In E-commerce domain Payment, Order, Products are some example of classes.

Class vs Object in Java

Some difference between class and object, which is totally based upon practical experience :

1) A class is what you create while coding, but object is created at runtime by your execution environment e.g. JVM. Though you write code, which is required to create object during coding e.g. new Student(), object is not created at that time. They are only created when you run your program, and when runtime executes that line. Usually constructor of a class is called when an object is created in Java, but yes there are some anomalies as well e.g. Serialization.

2) Most important difference between class and object is that an Object usually has state (though stateless object is also possible). This is used to differentiate with another object. For example, If you have a class to represent Student, then John and Mohan are two object of that class, which has different name, an attribute which differentiate them. A picture is worth more than 1000 words, and difference between class and object can be best explained by this image :

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

Here Player is a class which is actually the blueprint of creating players and two players Scooby and Tabby are objects which is created by runtime environment, Java Virtual Machine in this case. Each Player has different value for their attribute, also known as state of Object. For example Scooy's position is 5th and has $341, while Tabby's position is 18th and has $87.  If you still doesn't quite get difference between Class and Object then here is one more example, which you might find more interesting then this one.

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

Here CookieCutter is a class which is a blueprint to create objects, the cookies. You can see we have two cookies here, one for you and one for me :)

3) Though Java is not pure Object oriented language, most of things are object in Java, for example primitive variables and operator are not object. In Java, objects are created on a special memory area, known as heap memory. No matter on which scope you create them e.g. locally or globally they are always created in heap space. On the other hand, classes are loaded into another special area of JVM memory, known as permgen space. From Java 8 onward, this space is also known as metaspace. You can create as many object from a class as you want, subject to your heap memory limit, because each object takes some memory. Once all the memory is exhausted, you can not create any more object and JVM will throw java.lang.OutOfMemoryError: Java Heap Space if you further try to create object.

4) Object's are also known as instances in Java programming language, and in JVM class is also represented by an instance of java.lang.Class. On the other hand class is also know as type. A reference variable which holds reference of an object have a type, which denotes what kind of object it can reference. For example in following code ConstructorDemo is name of the class, known as type here, cd is a reference variable of type ConstructorDemo, which means it can either point a ConstructorDemo object or it's child classes. When you create Object using new() operator it automatically class the constructor of that class as well.

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

5) Class is an abstraction to contain code, mostly attributes and methods which operate on them. On the other hand object are the real thing which operate on them, but there comes static methods, which belongs to class.