Object-oriented programming (OOP) is a paradigm that uses "objects" to design applications and programs. Java, being an object-oriented language, relies heavily on classes and objects. In this blog post, we’ll explore these core concepts and how to use them effectively.
What is a Class? 🏷️
A class in Java is a blueprint or prototype that defines the variables and methods (functions) common to all objects of a certain kind. It describes the attributes and behaviors of the objects created from it.
Example:
public class Car {
// Properties (attributes)
String color;
String model;
int year;
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Method (behavior)
public void displayDetails() {
System.out.println("Color: " + color + ", Model: " + model + ", Year: " + year);
}
}
In this example, Car
is a class with three attributes (color
, model
, and year
) and one method (displayDetails
). The constructor initializes the properties of the class.
What is an Object? 🔍
An object is an instance of a class. When a class is defined, no memory is allocated until an object of that class is created. Objects are created using the new
keyword.
Example:
public class Main {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car("Red", "Toyota", 2020);
Car car2 = new Car("Blue", "Honda", 2018);
// Calling method on objects
car1.displayDetails(); // Output: Color: Red, Model: Toyota, Year: 2020
car2.displayDetails(); // Output: Color: Blue, Model: Honda, Year: 2018
}
}
Here, car1
and car2
are objects of the Car
class. Each object has its own set of attributes defined by the class.
Key Concepts of Classes and Objects
- Class: A blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data.
- Object: An instance of a class. Objects have states and behaviors.
- Constructor: A special method used to initialize objects.
- Method: A block of code within a class that performs a specific task.
Advantages of Using Classes and Objects
- Modularity: The source code for a class can be written and maintained independently of the source code for other classes.
- Reusability: Once a class is written, it can be used to create multiple objects. This helps in reusing code.
- Scalability: As the requirements change, new objects can be created without changing existing code.
- Easy Maintenance: Changes in the class can be done independently, making the code more maintainable.
Practical Example
Let’s create a simple banking system using classes and objects. We’ll define a BankAccount
class to represent a bank account.
BankAccount Class:
public class BankAccount {
// Properties
private String accountNumber;
private double balance;
// Constructor
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// Methods
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Invalid deposit amount");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrew: " + amount);
} else {
System.out.println("Invalid withdraw amount or insufficient funds");
}
}
public void displayBalance() {
System.out.println("Account Number: " + accountNumber + ", Balance: " + balance);
}
}
Main Class to Use BankAccount:
public class Main {
public static void main(String[] args) {
// Creating a BankAccount object
BankAccount account = new BankAccount("123456789", 500.0);
// Performing operations on the account
account.deposit(150.0);
account.withdraw(100.0);
account.displayBalance(); // Output: Account Number: 123456789, Balance: 550.0
}
}
Wrap Up
Understanding classes and objects is fundamental to mastering Java and object-oriented programming. By defining classes and creating objects, you can write modular, reusable, and maintainable code.
🚀