Member-only story

Part 2: Object-Oriented Programming (OOP) in Java

Rishi
3 min readDec 11, 2024

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to design and build applications. OOP provides concepts like inheritance, polymorphism, encapsulation, and abstraction to model real-world scenarios effectively.

1. What is OOP?

OOP is centered around the concept of “objects,” which are instances of “classes.” These objects encapsulate both data (attributes) and methods (functions) that operate on the data.

2. Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class.

Example:

// Class definition
class Car {
String brand;
int year;

// Method
void displayInfo() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Create an object
Car car1 = new Car();
car1.brand = "Toyota";
car1.year = 2020;

// Access method
car1.displayInfo();
}
}

3. Constructors

A constructor is a special method used to initialize objects. It has the same name as the class and no return type.

--

--

Rishi
Rishi

Written by Rishi

Tech professional specializing in Java development and caching logic with expertise in SaaS and automation. https://rishi-preethamm.blogspot.com

No responses yet