Difference Between class and object in Java

In the real world, a class is like the design or blueprint of a product, and an object is the actual product with specific properties and behavior.

Sno. Class Object
1 Class is a template or blueprint or structure of the object Object is an instance of the class with actual value and perform actions through methods
2 Class is created only one time Objects are created multiple times
3 No memory is allocated for instance variables of a class until an object is created When object is created then memory is allocated to object in heap
4 For define the class structure , class keyword is used For making object , new keyword is used
5 Class contains only declarations, no actual data. Object contains actual data, which are values of instance variables.
6 Class contains the method definition. Object calls the methods to perform the action
7 Class is a user-defined data type Object is an instance of a user-defined data type (class).
8 Example : class Rectangle { int length; int width; } Example : Rectangle rect = new Rectangle(10, 20);
9 Class : car Object : BMW, Toyota, Suzuki, Skoda
Difference Between class and object in Java
Difference Between class and object in Java

let’s understand the concept of class and object with the help of the program. First, we define the class, and then we make the object of that class

Program Example for showing the Difference Between class and object in Java

// Define a class named Person
class Person {
    // Fields (attributes of the class)
    String name;
    int age;
    String phone;

    // Method to display the person's details
    void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Phone: " + phone);
    }

    // New method to check if the person is an adult
    boolean isAdult() {
        return age >= 18;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an object of the Person class
        Person obj = new Person();

        // Assign values to the fields of obj
        obj.name = "John";
        obj.age = 25;
        obj.phone = "833929202";

        // Call the displayInfo method to print the person's details
        obj.displayInfo();

        // Call the new method to check if the person is an adult
        if (obj.isAdult()) {
            System.out.println(obj.name + " is an adult.");
        } else {
            System.out.println(obj.name + " is not an adult.");
        }
    }
}

Explanation:

  • Person is the name of the class.
  • The Person class has 3 fields: name, age, and phone, and 2 methods: displayInfo() and isAdult().
  • An object obj is created in the main method and values are assigned to the name, age, and phone fields of obj.
  • Next, we call the displayInfo() method of obj, which prints the name, age, and phone of obj.
  • The displayInfo() method does not return any value because its return type is void.
  • After that, we check if obj is an adult using the isAdult() method.
  • The isAdult() method returns a value (true or false) because its return type is boolean.
  • If isAdult() returns true, the if block is executed; otherwise, the else block is executed.
Output
Name: John
Age: 25
Phone: 833929202
John is an adult.

Conclusion

The primary difference between classes and objects in Java lies in their nature and purpose. A class is a blueprint that defines the structure and behavior of objects, including fields and methods. In contrast, an object is an instance of a class that holds specific values for those fields. While a class represents a general concept, an object represents a concrete example of that concept in action. Understanding this distinction is crucial for effective object-oriented programming.

We’d Love to Hear From You!

Do you have questions or feedback about the difference between classes and objects in Java? We’re here to help! Whether you need further clarification, have suggestions, or simply want to share your thoughts, please leave a comment below. Your feedback is invaluable in helping us improve our content and support others in their learning journey. Don’t hesitate to reach out—we’re excited to hear from you!

Related Articles:

What Is Class In Java With Example?
What Is an Object in Java?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Aitechray