OOP Principles in Java
Object-oriented programming organizes code around objects. An object keeps related data and operations together. A class describes the kind of object we can create.
Why Do We Need This?
For a reading app, a book can have a title and an operation that shows its details. Keeping these together makes it easier to decide which part of the program is responsible for each action.
Main Ideas
| Concept | What it means |
|---|---|
| Encapsulation | Keep state controlled through a class and its methods |
| Inheritance | Create a class based on another class |
| Polymorphism | Use a common type with different implementations |
| Abstraction | Expose needed operations while hiding implementation details |
Simple Working Example
Run this program on its own. All required imports and classes are included.
class Book {
private final String title;
Book(String title) {
this.title = title;
}
void showTitle() {
System.out.println("Reading: " + title);
}
}
public class HelloWorld {
public static void main(String[] args) {
Book book = new Book("Java Practice");
book.showTitle();
}
}Output
Reading: Java PracticeBook groups a title and a method. The title is private, so outside code uses the class operations rather than directly changing that field. The following lessons explain each OOP principle separately.
Topics in This Section
Easy Way to Remember
Begin with clear responsibilities for objects. Inheritance is one tool for organizing relationships; it is not required for every class.
See you in next page....??