Skip to content
Related Articles

Related Articles

Difference Between Object And Class

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 May, 2022

Class is a detailed description, the definition, and the template of what an object will be. But it is not the object itself. Also, what we call, a class is the building block that leads to Object-Oriented Programming. It is a user-defined data type, that holds its own data members and member functions, which can be accessed and used by creating an instance of that class. It is the blueprint of any object. Once we have written a class and defined it, we can use it to create as many objects based on that class as we want. In Java, the class contains fields, constructors, and methods. For example, consider the Class of Accounts. There may be many accounts with different names and types,  but all of them will share some common properties, as all of them will have some common attributes like balance, account holder name, etc. So here, the Account is the class.

Object is an instance of a class. All data members and member functions of the class can be accessed with the help of objects. When a class is defined, no memory is allocated, but memory is allocated when it is instantiated (i.e. an object is created). For Example, considering the objects for the class Account are SBI Account, ICICI account, etc.

Fig-1: Pic Descriptions Class and object 

Fig-2: Class Diagram To Understand Class and Object


Java




/*
 * Demo of class and object.
 * Example: Bank Account
 */
 
//Step-1 : Define Class Account
class Account{
    //Step-2 : Declare instance variable or fields and make it privates
    private String name;
    private int id;
    private double balance;
    private double money;
     
    //Step-3 : Define Non-Parameterized Constructor
    public Account() {
     
    }
    //Step-4 : Define Parameterized Constructor
    public Account(String name, int id, double balance) {
         
        this.name = name;
        this.id = id;
        this.balance = balance;
    }
     
    //Step-5: Generate getter and setter
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    //Step-6: Generate toString() method
    @Override
    public String toString() {
        return "Account [name=" + name + ", id=" + id + ", balance=" + balance + "]";
    }
     
    //Step-7 : Add user-defined method-> balanceInquery()   
    public void balanceInquery() {
        System.out.println( name + " Current Balance Is :: " + balance );
    }
    
     
    //Step-8 : Add user-defined method-> withdrawMoney()
    public String  withdrawMoney() {
        return name + " Withdraw Money Successfully";
    }
     
}
 
public class AccountType {
 
    public static void main(String[] args) {
     //Step-9: Instantiate Objects Of class Account
        Account SBI = new Account("Raghab",2211,70000.00);
        Account ICICI = new Account("Navi",1001,90000.00);
         
    //Step-10: Access Attributes And Methods Of Class Account
     
    //For Account SBI ::
     System.out.println(SBI.toString());  //Access toString Method
     SBI.balanceInquery();    
     SBI.setMoney(5000); //Set money Raghab wants to withdraw
     
     System.out.println("Raghab Withdraw Money From SBI:: " + SBI.getMoney());
     System.out.println("Raghab Withdraw Money From SBI:: " + SBI.withdrawMoney());
     System.out.println("----------------------------------------------------------");
     
    //For Account ICICI ::
     System.out.println(ICICI.toString());   //Access toString Method
     ICICI.balanceInquery();   
     ICICI.setMoney(1000); //Set money Navi want to withdraw
      
     System.out.println("Navi Withdraw Money From ICICI:: " + ICICI.getMoney());
     System.out.println("Navi Withdraw Money From ICICI:: " + ICICI.withdrawMoney());
     System.out.println("----------------------------------------------------------");
 
    }
 
}

Difference Between Class And Object:

There are many differences between object and class. Some differences between object and class are given below:

ClassObject
Class is used as a template for declaring and 
creating the objects.      
An object is an instance of a class.
When a class is created, no memory is allocated.Objects are allocated memory space whenever they are created.
The class has to be declared first and only once.An object is created many times as per requirement.
A class can not be manipulated as they are not
available in the memory.
Objects can be manipulated.
A class is a logical entity.An object is a physical entity.
It is declared with the class keywordIt is created with a class name in C++ and 
with the new keywords in Java.
Class does not contain any values which 
can be associated with the field.
Each object has its own values, which are
associated with it.
A class is used to bind data as well as methods together as a single unit.Objects are like a variable of the class.

Syntax: Declaring Class in C++ is as follows: 

class <classname> {};

Syntax: Instantiating an object for a Class in C++ is as follows: 

class Student {

   public:

      void put(){

          cout<<“Function Called”<<endl;

      }

};   // The class is declared here

int main(){

         Student s1;   // Object created

         s1.put();

}

Example: BikeExample: Ducati, Suzuki, Kawasaki

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!