Java OOP Principles
· By Saurabh Editorial · Java
Encapsulation, inheritance, polymorphism, and abstraction — applied in idiomatic Java.
What is OOP?
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
Class vs interface
Object-oriented programming models software as collaborating objects, each bundling state and behavior. Java was designed around OOP, so understanding its four pillars is essential to writing idiomatic code.
Hide internal state behind a public interface. Mark fields private and expose controlled access through getters, setters, or domain methods. This protects invariants and makes refactors safer.
A subclass extends a superclass to reuse and specialize behavior. Use it for true 'is-a' relationships — a Manager is an Employee. Prefer composition over deep inheritance trees for everything else.
One interface, many implementations. A List reference can point to an ArrayList or LinkedList; calling list.add() dispatches to the right implementation at runtime. Method overloading and overriding are the two flavors.
Expose what an object does, hide how it does it. Abstract classes and interfaces define contracts; implementations are swappable. This is the foundation of dependency inversion.