Notes – Object Oriented Programming in Java
Java is known as a pure object-oriented programming language, meaning it is designed around the concept of objects and classes.
Letโs explore what this means in simple terms.
What is Object-Oriented Programming?
- OOP is a programming style where real-world entities like person, car, bank account are represented as objects.
- These objects contain data (fields/attributes) and behavior (methods/functions).
Example:
class Car {
String color;
void drive() {
System.out.println("Car is moving");
}
}
4 Main Pillars of OOP in Java
| Concept | Description |
|---|---|
| Encapsulation | Wrapping data (variables) and code (methods) together in one unit, i.e., a class. |
| Inheritance | One class (child) can reuse the features of another class (parent). |
| Polymorphism | One function behaves differently in different scenarios. |
| Abstraction | Hiding internal details and showing only the essential features. |
Java as an Object-Oriented Language
- Everything in Java revolves around classes and objects.
- Java uses OOP to structure code for better reusability, scalability, and maintenance.
Important Terms in OOP (Java)
| Term | Description |
|---|---|
| Class | Blueprint of an object; defines properties and methods. |
| Object | Instance of a class; occupies memory. |
| Method | A function defined inside a class. |
| Constructor | Special method used to initialize objects. |
| Access Modifiers | Keywords like public, private, and protected to control visibility. |
Why OOP is Important in Java?
- Helps organize code better.
- Reduces duplication and promotes code reuse.
- Makes complex applications easier to manage and scale.
Example in Real Life
Class: Dog
Object: Your pet dog, Max
Data (fields): name = โMaxโ, breed = โLabradorโ
Behavior (methods): bark(), sleep()
class Dog {
String name;
void bark() {
System.out.println(name + " is barking");
}
}
Summary
Java is object-oriented because:
- It supports all core OOP principles (Encapsulation, Inheritance, Polymorphism, Abstraction).
- Programs are structured using classes and objects.
- It improves modularity, reusability, and real-world modeling in programming.
