![]() |
VOOZH | about |
In Java, classes and objects form the foundation of Object-Oriented Programming (OOP). They help model real-world entities and organize code in a structured way.
For Example, Dog is a class, Tommy is an object of that class.
A class is a blueprint that defines data and behavior for objects. It groups related fields and methods in a single unit. Memory for instance members is allocated when an object is created, while static members are allocated when the class is loaded into memory.
10 Alice
Explanation: This code defines a Student class as a blueprint with data members and a constructor to initialize them. An object s1 is created in main(), which allocates memory and accesses the class fields to display values.
An object is an instance of a class created to access its data and operations. Each object holds its own state.
Objects mirror real-world items such as customer, product or circle. Non-primitive objects are stored on the heap while their references remain on the stack.
Creating an object is known as instantiation. All instances of a class share structure and behavior while storing different state values.
Declaration:
Dog tuffy;
This only declares a reference. The object is not created and the reference holds null.
Initialization:
tuffy = new Dog("Tuffy", "Papillon", 5, "White");
The new operator allocates memory and invokes the constructor.
Example: Defining and Using a Class
Name is: tuffy Breed age and color are: papillon 5 white
Explanation: This creates a Dog object named tuffy using the Dog constructor inside main(), which initializes name, breed, age, and color. When System.out.println(tuffy) is executed, the overridden toString() method is called to display the object’s data.
Note: Every class has at least one constructor. If none is defined Java provides a default no-argument constructor that calls the parent constructor.
Example: Initialize Object by using Method/Function
Visual Studio - 0.0 IntelliJ IDEA - 4999.0
Explanation: This code defines a Product class with instance variables name and price, which are initialized using a constructor. In the main() method, two objects (p1 and p2) are created with different values. Each object maintains its own state, and the getter methods are used to access and display the values of each object independently.
Java supports four standard approaches.
new Keyword is most direct way to create an object.
Used for dynamic class loading as seen in frameworks like Spring.
Student@1dbd16a6
clone() creates a copy of an existing object. The class must implement Cloneable.
GeeksForGeeks
Explanation: Here, creates a new Geeks object g2 by cloning the existing object g1 using the overridden clone() method. The cloned object copies the field values of g1, which is confirmed by printing g2.name.
De-serialization is a technique of reading an object from the saved state in a file. Object is recreated from a stored byte stream.
Student: Alice
Explanation: This first serializes a Student object to the file student.ser using ObjectOutputStream. It then deserializes the object using ObjectInputStream, recreating the Student object and printing it via the toString() method.
Anonymous objects are created without a reference and used immediately for one-time operations.
new Dog("Max", "Labrador", 3, "Black").getName();