Lesson 14 of 36Article16 min
Classes, Objects & Attributes
A class is a blueprint. An object is one instance built from that blueprint.
Classes & Objects
A class is a blueprint. An object is one instance built from that blueprint.
Create object with new: Student s = new Student();
Real-life example: Class = cookie cutter shape. Object = each actual cookie you bake from that cutter.
Class and object
class Car {
String brand;
int year;
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.year = 2024;
System.out.println(myCar.brand);
}
}Attributes — fields of a class
Attributes (fields) store object state — name, age, balance. Each object has its own copy of instance fields.
Access with dot: object.fieldName. Initialize in constructor or at declaration.
Real-life example: Attributes are labels on a student ID card — name, roll number, photo — unique per student.
Instance attributes
class Book {
String title = "Unknown";
String author;
double price;
void showInfo() {
System.out.println(title + " by " + author + " — ₹" + price);
}
}