If you have studied Object Oriented Programming(OOP), then you must be familiar with terms class, object and instance.
Classes and objects are two main characteristics of object oriented programming. The difference between class and object is given here:
What is class?
Class is a “template” or “blueprint” that is used to create objects. It defines the data and behavior of a type.
Class allows you to create own custom types by grouping together variables of different types, methods and events.
Class also supports main characteristic of object oriented programming i.e. inheritance.
A class consists of field, static field, method, static method and a constructor.
Field: Field of class holds the state of class i.e. name of employee object
Method: Methods represents behavior of a class, for example showing how the employee object is working.
Constructor: It is used to create new instance of a class.
Following is example of class declaration:
public class ClassName { //Fields, properties, methods and events go here... }
The scope of the class depends upon its declaration. If the class is declared static then only one copy exists in memory and client code can only access it through the class itself.
What is object?
Basically the object is an entity. If we discuss real world objects like human being, cars etc, each of these objects have two characteristics. For example, human being has
State: Name and age
Behavior: running, sleeping
If we take the example of object Car, then it also has have state (speed) and state (applying brake).
Similarly, when we discuss about objects in object oriented programming languages, each object has
Properties (Attribute or state)
Methods (Do something or perform action)
An object stores its state in fields and exposes its behavior through methods.
The object is a block of memory which is allocated and configured according to the blueprint. A class can create many objects which are stored in a named variable or in an array or collection. In object-oriented language such as C#, a typical program consists of multiple objects which are dynamically interacting.
Following is an example of creating an object and instance of a class:
public class employee { public string Name { get; set; } public int Age { get; set; } public employee(string name, int age) { Name = name; Age = age; } //Other properties, methods, events... } class Program { static void Main() { employee emp1 = new employee("First_Name", 25); Console.WriteLine("emp1 Name = {0} Age = {1}", emp1.Name, emp1.Age); Person emp2 = emp1; // Declare new employee, assign emp1 to it. emp2.Name = "Second_Name"; //Change the name of emp2 emp2.Age = 31; Console.WriteLine("emp2 Name = {0} Age = {1}", emp2.Name, emp2.Age); Console.WriteLine("emp1 Name = {0} Age = {1}", emp1.Name, emp1.Age); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: emp1 Name = First_Name Age = 25 emp2 Name = Second_Name Age = 31 emp1 Name = Second_Name Age = 31 */
In the above example, the employee is a type while emp1 and emp2 are instance or objects.
What is instance?
An instance is a unique copy of a Class representing an Object. When a new instance of a class is created, the JVM will allocate memory for that class instance.
The object is an instance of a class. The object and instance are the same things but term instance indicates the relationship of an object to a class. The term instance describes a relationship, not a thing.
In Java programming, there are categories of types, primitive and reference types. Reference types are further categories into classes and array types. A Java Object is an instance of a reference type. Although terms instance and objects are treated as synonyms but mostly programmers use word value rather than instance to refer to an instance of primitive type.
Differences
- Class and objects are main concepts of Object Oriented Programming, the class is a blueprint used to create same type objects while object is an entity.
- Class is a type while object is a variable.
- Class is an expanded concept of data structures and objects are the instantiation of a class.
- Class is a logical entity while object is a physical entity.
- When class is declared, no memory is allocated but when object of a class is declared, memory is allocated.
- Class creates objects and provides values for state and implementation of behavior while objects contain properties and methods. Properties are attributes of an object while methods provide functionality.
- The purpose of objects is data abstraction while that of a class is a grouping of data.
- Class does not represent any object — rather it represents information and method that an object should have.