Classes and Objects: The Foundation of OOP
Duration: 5 min
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of 'objects'. Instead of thinking about programs as sequences of commands, OOP thinks about them as collections of objects that interact with each other. This approach makes code more modular, reusable, and easier to maintain.
Classes and Objects
A class is a blueprint or template for creating objects. It defines the structure (attributes) and behavior (methods) that objects of that class will have. An object is an instance of a class - a concrete realization of the blueprint.
Think of it this way: A class is like a cookie cutter, and objects are the actual cookies made from that cutter.
public class Car {
// Attributes (data members)
private String brand;
private String color;
private int year;
private double speed;
// Constructor - initializes the object
public Car(String brand, String color, int year) {
this.brand = brand;
this.color = color;
this.year = year;
this.speed = 0;
}
// Methods (behaviors)
public void accelerate() {
speed += 10;
System.out.println(brand + " is accelerating. Speed: " + speed);
}
public void brake() {
speed = 0;
System.out.println(brand + " has stopped.");
}
public void displayInfo() {
System.out.println("Brand: " + brand + ", Color: " + color + ", Year: " + year);
}
}No output - this is a class definitionCreating and Using Objects
public class CarDemo {
public static void main(String[] args) {
// Create objects (instances of Car class)
Car car1 = new Car("Toyota", "Red", 2022);
Car car2 = new Car("Honda", "Blue", 2023);
// Call methods on objects
car1.displayInfo();
car1.accelerate();
car1.accelerate();
car1.brake();
System.out.println();
car2.displayInfo();
car2.accelerate();
}
}Brand: Toyota, Color: Red, Year: 2022
Toyota is accelerating. Speed: 10
Toyota is accelerating. Speed: 20
Toyota has stopped.
Brand: Honda, Color: Blue, Year: 2023
Honda is accelerating. Speed: 10Access Modifiers
Access modifiers control the visibility of class members:
• public: Accessible from anywhere
• private: Accessible only within the class
• protected: Accessible within the class and subclasses
• package-private (no modifier): Accessible within the same package
Use private for attributes to protect data and force access through methods (encapsulation).
Getters and Setters
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String name) {
this.name = name;
}
// Getter for age
public int getAge() {
return age;
}
// Setter for age with validation
public void setAge(int age) {
if (age > 0 && age < 150) {
this.age = age;
} else {
System.out.println("Invalid age");
}
}
}No output - this is a class definition💡 Tip: Getters and setters allow you to control how data is accessed and modified. You can add validation in setters to ensure data integrity.
Learn more: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
❓ What is the difference between a class and an object?
❓ What is a best practice when working with Classes and Objects?
💡 Tip: Pro Tip: Master Classes and Objects thoroughly. This foundation is crucial for writing professional Java code.