Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Difference Between VB.NET and C#
Next article icon

Difference Between Object And Class

Last Updated : 02 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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("----------------------------------------------------------");
 
    }
 
}
 
 

C#




// Include namespace system
using System;
 
 
// * Demo of class and object.
// * Example: Bank Account
// Step-1 : Define Class Account
public 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
    // 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 this.name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getId()
    {
        return this.id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public double getBalance()
    {
        return this.balance;
    }
    public void setBalance(double balance)
    {
        this.balance = balance;
    }
    public double getMoney()
    {
        return this.money;
    }
    public void setMoney(double money)
    {
        this.money = money;
    }
    // Step-6: Generate toString() method
    public String toString()
    {
        return "Account [name=" + this.name + ", id=" + this.id.ToString() + ", balance=" + this.balance.ToString() + "]";
    }
    // Step-7 : Add user-defined method-> balanceInquery()
    public void balanceInquery()
    {
        Console.WriteLine(this.name + " Current Balance Is :: " + this.balance.ToString());
    }
    // Step-8 : Add user-defined method-> withdrawMoney()
    public String withdrawMoney()
    {
        return this.name + " Withdraw Money Successfully";
    }
}
public class AccountType
{
    public static void Main(String[] args)
    {
        // Step-9: Instantiate Objects Of class Account
        var SBI = new Account("Raghab", 2211, 70000.0);
        var ICICI = new Account("Navi", 1001, 90000.0);
        // Step-10: Access Attributes And Methods Of Class Account
        // For Account SBI ::
        Console.WriteLine(SBI.toString());
        // Access toString Method
        SBI.balanceInquery();
        SBI.setMoney(5000);
        // Set money Raghab wants to withdraw
        Console.WriteLine("Raghab Withdraw Money From SBI:: " + SBI.getMoney().ToString());
        Console.WriteLine("Raghab Withdraw Money From SBI:: " + SBI.withdrawMoney());
        Console.WriteLine("----------------------------------------------------------");
        // For Account ICICI ::
        Console.WriteLine(ICICI.toString());
        // Access toString Method
        ICICI.balanceInquery();
        ICICI.setMoney(1000);
        // Set money Navi want to withdraw
        Console.WriteLine("Navi Withdraw Money From ICICI:: " + ICICI.getMoney().ToString());
        Console.WriteLine("Navi Withdraw Money From ICICI:: " + ICICI.withdrawMoney());
        Console.WriteLine("----------------------------------------------------------");
    }
}
 
 

Difference Between Class And Object:

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

Class Object
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 keyword It 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: Bike Example: Ducati, Suzuki, Kawasaki


Next Article
Difference Between VB.NET and C#
author
jhakrraman
Improve
Article Tags :
  • Articles
  • C++
  • Difference Between
  • Java
  • Class and Object
Practice Tags :
  • CPP
  • Java

Similar Reads

  • Difference between namespace and class
    Classes are data types. They are an expanded concept of structures, they can contain data members, but they can also contain functions as members whereas a namespace is simply an abstract way of grouping items together. A namespace cannot be created as an object; think of it more as a naming convent
    2 min read
  • Difference between Component and Object
    1. Component : A component is a collection of objects that furnish a set of offerings to different systems. They have many elements in frequent with objects.Components can also be run both locally or in a distributed fashion. Many examples of locally run components exist and are oftentimes used to s
    2 min read
  • Difference between Entity and Object
    When talking about databases and data modeling, it's important to understand the distinction between entities and objects. Both are necessary for a database management system's (DBMS) data administration and representation. An entity is a unique, recognizable real-world object or notion that is char
    4 min read
  • Difference Between VB.NET and C#
    Visual Basic .NET is a high-level programming language that was initially developed in 1991. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented such as object, class,
    2 min read
  • Difference between OODM and CDM
    The Object Oriented Data Modeling and Conceptual Data Modeling can be referred to as two different approaches used for designing databases and systems. They are useful techniques of organizing and structuring the data in use within applications though they have their differences by focusing and unde
    4 min read
  • Difference Between Structure and Class in C++
    In C++, a structure works the same way as a class, except for just two small differences. The most important of them is hiding implementation details. A structure will by default not hide its implementation details from whoever uses it in code, while a class by default hides all its implementation d
    3 min read
  • Difference Between Source Code and Object Code
    Anyone needs to have the background knowledge that is required when studying computer programming, especially that of the difference between source code and object code. Such terms are used when describing the process of developing software, compiling it, and its execution. In this article, you will
    6 min read
  • Difference Between Class.this and this in Java
    In java, Class.this and this might refer to the same or different objects depending upon the usage. this this is a reference variable that refers to the current object. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Class.this Cla
    3 min read
  • Difference between OOP and POP
    Object-Oriented Programming (OOP)OOP treats data as a critical element in the program development and does not allow it to flow freely around the system.In OOP, the major emphasis is on data rather than procedure (function).It ties data more closely to the function that operate on it, and protects i
    5 min read
  • Difference between Abstract Data Types and Objects
    1. Abstract data type (ADT) :An Abstract data type (ADT) is a mathematical model for data types. We can think of Abstract Data Type as a black box. As black box hides internal structure, ADT hides the design of data type. An abstract data type's behavior (semantics) can be specified from the perspec
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences