CLASS & OBJECT

Object is the physical as well as logical entity where as class is the only logical entity.
Class: Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.
A class in java contains:
  • Data Member
  • Method
  • Constructor
  • Block
  • Class and Interface
Object: Object is a instance of class, object has state and behaviors.
An Object in java has three characteristics:
  • State
  • Behavior
  • Identity
State: Represents data (value) of an object.
Behavior: Represents the behavior (functionality) of an object such as deposit, withdraw etc.
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 JVM to identify each object uniquely.
Class is also can be used to achieve user defined data types.

Difference between Class and Object

ClassObject
1Class is a container which collection of variables and methods.object is a instance of class
2No memory is allocated at the time of declarationSufficient memory space will be allocated for all the variables of class at the time of declaration.
3One class definition should exist only once in the program.For one class multiple objects can be created.

Syntax to declare a Class

 class Class_Name
  {  
    data member;  
    method;  
  }  

Simple Example of Object and Class

In this example, we have created a Employee class that have two data members eid and ename. We are creating the object of the Employee class by new keyword and printing the objects value.

Example

class Employee
{
 int eid;  // data member (or instance variable)
 String ename;  // data member (or instance variable)
 eid=101;
 ename="Hitesh";
 public static void main(String args[])
 { 
  Employee e=new Employee(); // Creating an object of class Employee
  System.out.println("Employee ID: "+e.eid);
  System.out.println("Name: "+e.ename);
 }  
}  

Output

Employee ID: 101
Name: Hitesh
Note: A new keyword is used to allocate memory at runtime, new keyword is used for create an object of class, later we discuss all the way for create an object of class.

There are many ways to create an object in java. They are : 

  • By new keyword
  • By newInstance() method
  • By clone() method
  • By factory method etc.
We will learn, these ways to create the object later.

Annonymous object

Annonymous simply means nameless.An object that have no reference is known as annonymous object.
If you have to use an object only once, annonymous object is a good approach.
  1. class Calculation{  
  2.   
  3.  void fact(int  n){  
  4.   int fact=1;  
  5.   for(int i=1;i<=n;i++){  
  6.    fact=fact*i;  
  7.   }  
  8.  System.out.println("factorial is "+fact);  
  9. }  
  10.   
  11. public static void main(String args[]){  
  12.  new Calculation().fact(5);//calling method with annonymous object  
  13. }  
  14. }  
Output:Factorial is 120

Creating multiple objects by one type only

We can create multiple objects by one type only as we do in case of primitives.
  1. Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects  
Let's see the example:
  1. class Rectangle{  
  2.  int length;  
  3.  int width;  
  4.   
  5.  void insert(int l,int w){  
  6.   length=l;  
  7.   width=w;  
  8.  }  
  9.   
  10.  void calculateArea(){System.out.println(length*width);}  
  11.   
  12.  public static void main(String args[]){  
  13.   Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects  
  14.     
  15.   r1.insert(11,5);  
  16.   r2.insert(3,15);  
  17.   
  18.   r1.calculateArea();  
  19.   r2.calculateArea();  
  20. }  
  21. }  
Output:55 
       45     

No comments:

Post a Comment