All four are enforced by language features, which is worth seeing in one place.
If you are still building the foundations, the object and class topics cover what an object actually is at runtime and how a class is put together.
Frequently Asked Questions
What are the four pillars of object-oriented programming?
Encapsulation, abstraction, inheritance, and polymorphism. Encapsulation keeps state private and validated by the object that owns it. Abstraction exposes a contract while hiding implementation. Inheritance reuses and specialises an existing type. Polymorphism lets one call site work correctly against many concrete types.
Do getters and setters give me encapsulation?
Only if the setters enforce something. A getter and setter pair that assigns whatever it is handed removes the protection the private field gave you. Encapsulation means the object validates every change to its own state, so either the setter checks a rule or the field is made final and there is no setter.
How is abstraction different from encapsulation?
Encapsulation is about protecting data inside one object, keeping fields private and routing changes through validating methods. Abstraction is about reducing coupling between types, publishing a contract such as an interface and hiding how it is implemented. They are related, but one guards state and the other guards dependencies.
Which form of polymorphism does Java use?
Java uses both. Compile-time polymorphism is method overloading, where the compiler selects the method from the declared parameter types. Run-time polymorphism is method overriding with dynamic dispatch, where the JVM selects the method from the object actual type. The second is the one that removes type checks from calling code.
Can a Java class inherit from more than one class?
No. A class can extend at most one class, so there is no diamond inheritance problem to resolve. It can implement any number of interfaces, and when two interfaces provide the same default method you must override it explicitly to say which one you want.
Why prefer composition over inheritance?
Because inheritance permanently couples a subclass to the internals of its base class. Any new method added to the base can collide with a method you wrote, and the subclass is pinned to that shape forever. Composition holds an instance instead, so you can swap the implementation later without any subclass inheriting the change.
Do I need all four pillars in every class?
No. A simple immutable value class such as a record needs no inheritance and no polymorphism to be well designed. Apply the principles where the problem calls for them. Forcing a hierarchy onto code that does not need one adds coupling without removing any.
How do I check whether a class really encapsulates its state?
Look for fields that are private and final wherever possible, and check every setter for an actual validation rule. Then list the invariants the class promises, such as a non-negative balance, and confirm that no method can produce a state that breaks one. If a caller could put the object into an invalid state, encapsulation is missing.