Wednesday, 19 October 2016

Classes and Objects in Java:

Object in general terms is anything that physically exist in the world.
Class is a blueprint of the object.

Here is a example below which gives you a easy hint to understand Class and Object.

Example: Flower is a generic term that does not exist in the world. Rose, lotus..etc only exist in the world which is physically present and that is the object. Here the term flower is the class.

Class contains member variables and methods. This can be accessed only with the help of object.

Object is the instance of the class.
Object creation only will allocate memory for the class members.
Object is also called as instance.

Example of the class:

class ExampleClass{
  public String name = "Sam";
}

class Caller{
 public static void main(String args[]){
    ExampleClass ec = new ExampleClass();//Object is created for the class
    System.out.println(ec.name);//Member of the class is access with the object
 }
}