1. Encapsulation
Encapsulation protects the internal state of an object by restricting direct access using private fields. Access is provided through getter and setter methods.
public class Person {
private String name; // hidden data
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age >= 0) {
this.age = age;
}
}
public static void main(String[] args) {
Person p = new Person();
p.setName("John");
p.setAge(25);
System.out.println(p.getName());
System.out.println(p.getAge());
}
}2. Inheritance
Inheritance allows one class (child) to acquire fields and methods from another class (parent). This promotes code reuse and method overriding.
class Vehicle {
protected String brand = "Honda";
public void start() {
System.out.println("Vehicle is starting...");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("Car is driving...");
}
public static void main(String[] args) {
Car c = new Car();
c.start();
c.drive();
System.out.println(c.brand);
}
}3. Polymorphism
Polymorphism allows the same method to behave differently depending on the object that invokes it.
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Cat says: Meow");
}
}
class Dog extends Animal {
public void sound() {
System.out.println("Dog says: Woof");
}
}
public class TestPoly {
public static void main(String[] args) {
Animal a1 = new Cat();
Animal a2 = new Dog();
a1.sound(); // Cat version
a2.sound(); // Dog version
}
}4. Abstraction
Abstraction hides implementation details and exposes only essential functionality. Achieved using abstract classes and interfaces.
A. Abstract Class
abstract class Shape {
abstract double area();
public void display() {
System.out.println("This is a shape");
}
}
class Circle extends Shape {
double radius = 5;
double area() {
return 3.14 * radius * radius;
}
public static void main(String[] args) {
Circle c = new Circle();
c.display();
System.out.println("Area = " + c.area());
}
}B. Interface
interface Animal {
void sound();
void sleep();
}
class Lion implements Animal {
public void sound() {
System.out.println("Lion roars");
}
public void sleep() {
System.out.println("Lion sleeps");
}
public static void main(String[] args) {
Lion l = new Lion();
l.sound();
l.sleep();
}
}Best Practices of OOP in Java
- Use encapsulation to protect internal data.
- Apply inheritance only when classes share real relationships.
- Use polymorphism to simplify conditional logic.
- Prefer interfaces when defining behavior without implementation.
- Keep classes focused on a single responsibility.
6. OOP Concepts Explained with a Real-Life Example
Understanding OOP becomes much easier when we relate it to real-world scenarios. Consider a simple example of a Bank Account.
- Encapsulation: Account balance is private and cannot be modified directly. Users must use deposit or withdraw methods.
- Inheritance: SavingsAccount and CurrentAccount inherit common properties from Account.
- Polymorphism: The same
calculateInterest()method behaves differently for savings and current accounts. - Abstraction: Users interact with services like transferMoney without knowing internal validation logic.
This design improves security, flexibility, and maintainability in large financial systems.
7. Abstract Class vs Interface (Key Differences)
Both abstract classes and interfaces are used to achieve abstraction, but they serve different purposes.
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Abstract + Concrete | Only abstract (default allowed) |
| Multiple Inheritance | No | Yes |
| Variables | Any type | public static final |
| Use Case | Base class with common logic | Define behavior contract |
Use abstract classes when classes share common code. Use interfaces when multiple unrelated classes need the same behavior.
8. Common OOP Mistakes Beginners Should Avoid
- Making all variables public instead of using encapsulation
- Using inheritance where composition is more suitable
- Overusing abstract classes unnecessarily
- Writing large classes with multiple responsibilities
- Ignoring interfaces for flexible design
Avoiding these mistakes early will help you write clean, professional-grade Java code.
9. Why OOP Matters in Interviews and Industry
OOP concepts are heavily tested in Java interviews because they reflect how developers think about system design.
In real-world Java applications such as ERP systems, banking platforms, Android apps, and microservices:
- Encapsulation ensures data safety
- Inheritance reduces duplicate code
- Polymorphism simplifies complex logic
- Abstraction improves scalability
Strong understanding of OOP directly impacts your ability to build maintainable enterprise software.
10. Summary
In this chapter, you explored the four core pillars of Java Object-Oriented Programming: Encapsulation, Inheritance, Polymorphism, and Abstraction.
These principles form the foundation of modern Java development and are essential for building scalable, secure, and maintainable applications.
Next, you will learn how Java organizes large applications using Packages.