Member-only story
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.