Object & class 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. It can be physical or logical (tangible and intangible). The example of intangible object is banking system.
An object has three characteristics:
  • 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.
For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.
Object Definitions:
  • Object is a real world entity.
  • Object is a run time entity.
  • Object is an entity which has state and behavior.
  • Object is an instance of a class.

Class in Java

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
  • fields
  • methods
  • constructors
  • blocks
  • nested class and interface

Syntax to declare a class:

  1. class <class_name>{  
  2.     field;  
  3.     method;  
  4. Example:
  5. class Student{  
  6.  int id;//field or data member or instance variable  
  7.  String name;  
  8.   
  9.  public static void main(String args[])
  10.  {  
  11.   Student s1=new Student();//creating an object of Student
  12.   s1.id=101;  
  13.   s1.name="Sonoo";  
  14.   System.out.println(s1.id);//accessing member through reference variable  
  15.   System.out.println(s1.name);  
  16.  }  
  17. }  
  18.  output:










    We can also create multiple objects and store information in it through reference variable.:
    class Student{  
     int id;  
     String name;  
    }  
    class TestStudent{  
     public static void main(String args[]){  
      //Creating objects  
      Student s1=new Student();  
      Student s2=new Student();  
      //Initializing objects  
      s1.id=101;  
      s1.name="Sonoo";  
      s2.id=102;  
      s2.name="Amit";  
      //Printing data  
      System.out.println(s1.id+" "+s1.name);  
      System.out.println(s2.id+" "+s2.name);  
     }  
    }  
    output:






    Object and Class Example: Initialization through method

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

Another Example :

class Employee {

   String name;
   int age;
   String designation;
   double salary;

   // This is the constructor of the class Employee
   public Employee(String name) {
      this.name = name;
   }

   // Assign the age of the Employee  to the variable age.
   public void empAge(int empAge) {
      age = empAge;
   }

   /* Assign the designation to the variable designation.*/
   public void empDesignation(String empDesig) {
      designation = empDesig;
   }

   /* Assign the salary to the variable salary.*/
   public void empSalary(double empSalary) {
      salary = empSalary;
   }

   /* Print the Employee details */
   public void printEmployee() {
      System.out.println("Name:"+ name );
      System.out.println("Age:" + age );
      System.out.println("Designation:" + designation );
      System.out.println("Salary:" + salary);
   }
}

public class EmployeeTest {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      Employee empOne = new Employee("James Smith");
      Employee empTwo = new Employee("Mary Anne");

      // Invoking methods for each object created
      empOne.empAge(26);
      empOne.empDesignation("Senior Software Engineer");
      empOne.empSalary(1000);
      empOne.printEmployee();

      empTwo.empAge(21);
      empTwo.empDesignation("Software Engineer");
      empTwo.empSalary(500);
      empTwo.printEmployee();
   }
}
Previous
Next Post »