Thursday, 30 July 2015

What is a class ?

 A class is the blueprint from which individual objects are created. The fields represent the object's state, and the methods define its interaction with the outside world. Classes are categories, and objects are items within each category.Classes are templates that are used to create objects, and to define object data types and methods.Now we focus on what is object in java

Object in java :-


An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc.


An object has three characteristics :-

  1.    state: represents data (value) of an object.
  2.    behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
  3.    identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the Java Virtual Machine to identify each object uniquely.      
For Example: Car is an object. Its name is Duster, color is white etc. known as its state. It is used to ride, so riding from one place to another is its behavior.
Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

 Another Real life Example: a tree should have branches, stems and leaves. Thus, if Banyan is a tree, Banyan should have all of the characteristics of a tree, such as branches, stems and leaves. It is impossible to say that a pigeon is a tree, because the pigeon does not have branches, stems and leaves. Similarly, basic Java object properties are defined within that object’s corresponding class.

So,
Object is an instance or result of a class .

Class in java :-

A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.

Syntax for declaring a class 

class <class_name>{  
    data member;  //State 
    method;            //behaviour
}  


Data member or instance variable :-
Instance variable doesn't get memory at compile time.It gets memory at runtime when object(instance) is created.That is why, it is known as instance variable.


Method or behaviour of objects :-
method is like function and used to expose behaviour of an object .Advantages of method are code reusability and code optimization .

new keyword is used to allocate memory at run time .
 Student2 s1=new Student2();  

Creating an Object :-


There are three steps when creating an object from a class:

DeclarationA variable declaration with a variable name with an object type.
InstantiationThe 'new' key word is used to create the object.
InitializationThe 'new' keyword is followed by a call to a constructor. This call initializes the new object
Now we take an example that illustrates the concepts of class as well as objects and this example maintains the records of students.

In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method on it. Here, we are displaying the state (data) of the objects by invoking the displayInformation method.

class Student2{  

 int rollno;  
 String name;  
  
 void insertRecord(int r, String n){  //method  
  rollno=r;  
  name=n;  
 }  
  
 void displayInformation(){System.out.println(rollno+" "+name);}//method  
  
 public static void main(String args[]){  
  Student2 s1=new Student2();  
  Student2 s2=new Student2();  
  
  s1.insertRecord(111,"Karan");  
  s2.insertRecord(222,"Aryan");  
  
  s1.displayInformation();  
  s2.displayInformation();  
  
 }  
}

Output :-

111 Karan
222  Aryan


this diagram explains concepts more clearly