A class is a blueprint for creating objects. It defines the state (fields) and behavior (methods) of objects.
class Car {
String color;
int speed;
void drive() {
System.out.println("Driving...");
}
}
Car myCar = new Car();
myCar.color = "Red";
myCar.drive(); // "Driving..."
Class Components
Component | Example |
---|---|
Fields | String name; |
Methods | void speak() { ... } |
Constructor | Car() { ... } |
Blocks | Static / instance initializer blocks |
Nested Classes | Inner or static nested classes |
Comparison
Class vs Object
Term | Meaning |
---|---|
Class | Blueprint / definition |
Object | Instance of a class (real-world item) |
Types of Classes in Java
Type | Example Use Case |
---|---|
Regular | Standard usage |
Abstract Class | Partial implementation |
Final | Cannot be extended |
Anonymous Class | One-time use, inline declaration |
Inner/Nested | Grouped logic within outer class |
Tip
Be able to explain the structure of a class, how to instantiate objects and the difference between class vs object.
Also expect questions on constructors, access modifiers or OOP principles via classes.
Parent: _OOP