OOP
Class & Object
Class & Object
1. Class
A class is a blueprint/template used to create objects.
It defines the object's data (fields) and behavior (methods).
class Car {String brand;int speed;void drive() {System.out.println("Car is driving");}}
| Part | Example | Purpose |
|---|---|---|
| Class | Car | Blueprint |
| Field | String brand | Stores data |
| Field | int speed | Stores data |
| Method | drive() | Defines behavior |
2. Object
An object is an instance of a class that contains its own state and can perform behaviors defined by the class.
Objects are created using the new keyword.
Car car1 = new Car();car1.brand = "BMW";car1.speed = 120;car1.drive();
| Concept | Example |
|---|---|
| Class | Car |
| Object | car1 |
| Create object | new Car() |
| Access field | car1.brand |
| Call method | car1.drive() |
4. Class vs Object
| Class | Object |
|---|---|
| Blueprint/template | Instance of a class |
| Logical definition | Real entity created from class |
| Does not represent one specific instance | Represents a specific instance |
Example: Car | Example: car1 |
Created using class | Created using new |
Next TopicConstructor, this, static