In Java, the fundamental building blocks of object-oriented programming are classes and objects. These concepts help us to model real-world entities and their interactions in a systematic and organized way.
A class in Java serves as a blueprint or template for creating objects. It encapsulates data for the object and methods to manipulate that data. Essentially, a class defines the properties (attributes) and behaviors (methods) that its objects will have.
An object is an instance of a class. While a class is a conceptual entity, an object is a concrete entity that exists in memory during runtime. Objects have state and behavior, which are defined by their class.
A class in Java is a user-defined data type that acts as a blueprint for objects. It defines the attributes and behaviors that the objects created from the class will have. Classes encapsulate data and operations on data into a single unit.
The primary purpose of a class is to represent real-world entities in Java applications. By using classes, we can create multiple objects that share the same properties and behaviors.
public class Student {
// Attributes (Fields)
private String name;
private int age;
private String studentId;
// Constructor
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
}
// Methods
public void study() {
System.out.println(name + " is studying.");
}
public void attendClass() {
System.out.println(name + " is attending class.");
}
// Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
}
An object is an instance of a class. It is a concrete entity that exists in memory during runtime. Objects have a state (defined by the values of their attributes) and behavior (defined by the methods of the class).
State: The state of an object is the values of its attributes at any given time.
For example
for a Student object, the state could be the name, age, and student ID.
Behavior: The behavior of an object is the actions that the object can perform, as defined by the methods of the class. For example,
a Student object can perform actions such as studying or attending a class.
Object is Real, Class is Virtual
While a class is a virtual concept (a blueprint), an object is a real entity that exists during the execution of a program.
Continuing with the house analogy, an object is like an actual house built from the blueprint. Each house (object) built from the same blueprint (class) can have different states (e.g., different paint colors, furniture) but follows the same design.
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
// Creating an object of the Student class
Student student1 = new Student("Alice", 20, "S12345");
// Accessing attributes and methods of the object
System.out.println("Student Name: " + student1.getName());
System.out.println("Student Age: " + student1.getAge());
System.out.println("Student ID: " + student1.getStudentId());
student1.study();
student1.attendClass();
// Modifying attributes of the object
student1.setAge(21);
System.out.println("Updated Student Age: " + student1.getAge());
}
}
Feature | Class | Object |
---|---|---|
Definition | Blueprint or template for creating objects | Instance of a class |
Nature | Conceptual entity | Physical entity that exists in memory |
Creation | Defined using class keyword | Created using new keyword |
Attributes | Defines attributes (fields) for objects | Holds specific values in attributes |
Methods | Defines behaviors (methods) for objects | Can invoke methods defined in the class |
Memory Allocation | No memory allocation at definition | Memory is allocated when an object is created |
Example | public class Car { String color; void drive() { ... } } | Car car1 = new Car(); car1.color = "Red"; car1.drive(); |