What is an Object?
About
Characteristics of an Object
Example of an Object
// Class Definition
class Car {
String brand; // Attribute
int speed;
// Constructor
Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
// Method (Behavior)
void accelerate() {
speed += 10;
System.out.println(brand + " is now going at " + speed + " km/h.");
}
}
// Main Class
public class Main {
public static void main(String[] args) {
// Creating Objects
Car car1 = new Car("Toyota", 50);
Car car2 = new Car("Honda", 40);
// Accessing Object Methods
car1.accelerate(); // Toyota is now going at 60 km/h.
car2.accelerate(); // Honda is now going at 50 km/h.
}
}How Objects Work in Memory
When is the object created with new keyword
Last updated