The knowledge about the difference between class and object question is the requirement for a candidate to attend a programming interview that involves any object oriented programming. Every programmer regard the terms, object and class as pillars of OOPS.
Class can be defined as the blueprints and objects are existing things that are created from the class:
The above the said definition may help in exams but it won’t help you during interview. Class and objects can be explained through this example: if you consider bike as a class, then Yamaha, Honda and BMW are considered as objects. Generally nouns are described as class in object oriented programming.
- Many: Java interview Q&A available here
Difference between Class and Object in Java
- Classes are created during the coding process while objects are created during the run-time execution of the program. In Java Virtual Machine; if you use create new class like employee (), objects for this class are not created immediately but created during when runtime executes that coding line. Mostly a constructor will be called for this particular class during the creation of the object. The anomalies created during the creation of the object are called serialization.
- Mostly all the objects created have a state but sometimes stateless objects are also created. States are used to differentiate an object from other objects. For example if you consider employee () as a class, then Neo and Trinity are the objects of this particular class. Both these objects have different names which can be accounted to differentiate these objects.
- Related: How the System.out.println() is working in Java?
Both the employees Neo and trinity may belong to different hierarchy so their salary will also be different. If we consider that Neo is a manager and Trinity as a Programmer and their salaries are $1000 and $ 500 respectively. Java virtual machine attribute different value for the both the employees and the attributed value is called the state of the object. According to the above given example Neo is a manager and earns $1000 while Trinity is a programmer and earns $ 500. Here the both Neo and Trinity are employees which is a class and their names are objects. The designation and salaries of Neo and Trinity are the state of the objects.
- Unlike C++, Java is not a complete object oriented programming language. For ex: operators and primitive variables used in Java are not object oriented. In JVM objects are created in a memory called heap memory. The classes creating during the coding are stored in permgen space. The name of permgen space is changed into meta space from Java 8. Any number of objects can be created for a particular class but there is a limit for permgen space. If the memory limit in permgen space is exceeded, JVM will give an error message: java.lang.outofmemoryerror: java heap space.
- In Java programming language objects are also called as instances. JVM represent class through an instance java.lang.Class.
- Classes are also called as type. Every reference variable which has a reference of an object has a type. This means the type of an object it can reference.
Let us consider the following code for an example
Employee ceo = new Employee();
In this code Employee is the name of the type and ceo is the reference variable of the class Employee. This means the reference variable can either point towards the object of the type Employee or the child class of Employee. When the object is created using a new(); operator it automatically calls the constructor of that particular class. In this case the constructor is Employee.
- Class is an abstraction which contains the code and every time it references and methods operated on it. Objects are real thing which operate on the class but the use of overloading static methods belong to the class.
- There is a life span for an object but classes the lifespan of the classes are not limited. As given in our earlier example the objects like Neo and Trinity may after sometime because someone might replace them as the manager and programmer but the class employee will always remain same.
As I referenced earlier classes are like blueprint but objects are real things which are created from classes. If a question is asked about the difference between class and objects, try to explain it along with an example. In that way the interviewer can easily understand the message you are trying to convey.