Friday, December 26, 2014

Object and Class in Java

Object in java:

The objects are the physically existing entities in java which are used for specific purpose.

Every object has certain state and behaviour, and will perform specific action.
An object has three characteristics:
  • State: represents data of an object.
  • Behaviour: represents the 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.

Object is an instance of a class. Class is a template or blueprint from which objects are created.

Class in java

A class is a core component of  java because without a class we cannot write a single statement in java.A class is a collection of objects that have the same properties. Class contains the fallowing members

  • data member
  • method
  • constructor
  • block
syntax
class <class name>{
//blocks
//constructors
//variables
//methods
}

 Example of Object and Class

In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value.
class Employee{ 
int  eno;  // instance variable declaration
String  ename; // instance variable declaration
public static void main(String ar[]){   // main method
Employee e=new Enployee();    // creating object to employee class
System.out.println(e.eno); // displaying employee number  of  employee class
System.out.println(e.ename);  // displaying employee name  of employee class
}
}
Out-put:
0 
null

No comments:

Post a Comment