Java Classes and Objects | Difference between Class and Object in java ?

           class and object in java are as follows:

  • The Object oriented programming synopsis are majorly used for code reusability purpose. The basic unit in OOPs are class and object in java. The characteristic of object oriented programmings are as follow:
    • 1.  Class: Class is a basic unit in object oriented programming concept. Class is a collection of objects or a blue print of an object. It can be stored data part and method part.
    • 2. Object: Object is an instance of class which exist physically having some common properties like state and behaviour.
    • 3. Encapsulation: Wrapping up of data and methods in a single unit is known as encapsulation.
    • 4. Inheritance: Getting the properties from super class to subclass.
    • 5.Abstraction: Hiding of un-neccessary data is known as Abstraction.
    • 6.Polymorphism: Ability to take more than one form is known as Polymorphism.

       Class

      Class is a basic unit in object oriented programming concept. Class is a collection of objects or a blue print of an object. It can be stored data part as well as method part. The class is declared by the use of class keyword. The class name should begin with a Capital letter.

    • Syntax:
    • class Classname
      {
      datatype variable 1;
      datatype variable 2;
      .
      .
      datatype variable n;
      datatype methodname1(parameter list)
      {
      //body of method;
      }
      datatype methodname2(parameter list)
      {
      //body of method;
      }
      .
      .
      datatype methodnamen(parameter list)
      {
      //body of method;
      }
      }
      

       

      Characteristic of Class:

      1.  We can declare class as either public or default type.

      2. We can declare class as either private or protected type.

      3. We can not declare class as static type.

      4. We can declare class as abstract or final but combination of both ( means abstract final or final abstract ) are not supported.

       Object

      Object is an instance of class which exist physically having some common properties like state and behaviour.

      syntax:

      Classname objectname = new Classname();

       Example:

      import java.io.*;
      import java.util.*;
      class Bike
      {
      Scanner sc=new Scanner(System.in);
      int price;
      void input()
      {
      System .out.println("Enter Bike price");
      price=sc.nextInt();
      }
      void display()
      {
      System .out.println("The Bike price is = "+price);
      }
      }

       

      Above class can be accessed from other class with the help of object.

      Class customer
      {
      public static void main(String args[])
      {
      Bike b1= new Bike();
      b1.input();
      b1.display();
      }
      }

      There are five different ways to create objects in java.

  • 1. Using newInstance() method:
Class c= class.forName("packageName.ClassName");
ClassName object=(ClassName)c.newInstance();

 

  • 2. Object deserialization:
ObjectInputStream instream= new ObjectInputStream(anstream);
ClassName object=(ClassName) instram.readObject();

 

  • 3. Using clone() method:
ClassName object1=new classname();
ClassName object2=object1.clone();

 

  • 4.Creating String and Array Objects:
String s="String_Object;

int[] x={77,8,9,5};

 

  • 5. Using new operator:
ClassName objectname = new ClassName();

 

  • A list of difference between class and Object in java are as follows…

class and object in java

  • Class

    Object

     1. Class is  a blue print of an object.  1. An object is an instance of a class.

     

     2.Class contain dis-similar data.  2. Object contain similar as well as dis-similar data.
     3.When class is created, no memory is allocated.  3. When object is created, memory is allocated.
     4. No such method is available for class.  4. we can create object for a class using different methods like clone(), newInstance() .
     5. Classes have logical existence.  5. Object have physical existence.
     6. Class is created using class keyword.  6. Object is created using new keyword.
     7. The class declared only once.  7. Object declared many times depending on the requirement.
     8.Class declares the data members(variables).  8.Every data member contains its own value.
     9.Syntax:

    class Classname

    {

    //variable;

    //methods;

    }

     9.Syntax:

    Classname Objectname= new Classname();

     

     

     

     

    10. Example:

    class Bike

    {

    }

    10. Example:

    Bike b1= new Bike();

     

     

    Read More:

  • difference between Abstract Class and Interface.

Leave a Comment