Java OOP Concepts Explained: Encapsulation, Inheritance, Polymorphism and Abstraction
The four object-oriented pillars in Java with short examples, the difference between abstract classes and interfaces, and why composition often beats inheritance.
SmartCampus Buddy TeamSeptember 14, 20269 min read
Object-oriented programming (OOP) is the reason Java code is organised into classes and objects. Interviewers and exams ask about its four pillars constantly, and Spring Boot and most Java frameworks assume you understand them. Here is each idea with a small example.
Classes and objects
A class is a blueprint that describes data (fields) and behaviour (methods). An object is one instance of that blueprint created with new.
class Account {
private double balance;
void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
balance += amount;
}
double getBalance() { return balance; }
}Encapsulation
Encapsulation means keeping fields private and exposing controlled access through methods. In the example, callers cannot set balance to a negative number directly; they must go through deposit, which validates the input. The benefit is that the class stays in charge of its own rules, and you can change its internals without breaking the code that uses it.
Inheritance
Inheritance lets a class reuse and extend another class using extends.
class Animal {
String sound() { return "..."; }
}
class Dog extends Animal {
@Override
String sound() { return "Woof"; }
}A Dog is an Animal. Java allows a class to extend only one class. Use inheritance for a true "is-a" relationship, not just to reuse code.
Polymorphism
Polymorphism means one reference type can refer to different actual objects, and the correct method runs at runtime.
Animal a = new Dog();
System.out.println(a.sound()); // WoofThe variable is declared as Animal, but the object is a Dog, so Dog.sound() runs. This is method overriding, or runtime polymorphism. Overloading (same method name, different parameters) is resolved at compile time. The @Override annotation asks the compiler to confirm that you really are overriding something, which catches typos.
Abstraction
Abstraction hides detail and shows only what a caller needs. In Java you do this with abstract classes and interfaces.
- An interface describes a contract of methods. A class can implement many interfaces.
- An abstract class can hold fields and constructors and share code, but cannot be instantiated. A class can extend only one.
Choose an interface when you want to describe a capability that unrelated classes can share, for example Comparable. Choose an abstract class when related classes share state and behaviour.
Composition over inheritance
Deep inheritance trees are hard to change. A common guideline is to prefer composition: a class holds a reference to another object and delegates work to it. A Car has an Engine rather than being an Engine. Composition keeps classes smaller and easier to test, and it is the style you will see throughout Spring applications.
Common mistakes
- Making fields public instead of private.
- Using inheritance only to reuse a few methods.
- Forgetting that constructors chain: a subclass constructor calls the superclass constructor first.
- Confusing overriding with overloading.
Test yourself
The Object-Oriented Java quiz covers polymorphism, static, final and constructor order with explained answers, and the Java Code Lab lets you practise on interactive problems. If strings and equality still trip you up, read Java == vs equals().
Key takeaways
- Encapsulation protects data behind methods.
- Inheritance models is-a relationships; Java allows single class inheritance.
- Polymorphism runs the actual object's method at runtime.
- Abstraction uses interfaces and abstract classes.
- Prefer composition when in doubt. Next, see how these ideas appear in Spring Boot controller, service and repository layers.