As a Java software engineer, mastering Object-Oriented Programming (OOP) is essential. Java was built with OOP principles at its core, and understanding them helps you write cleaner, modular, and scalable code.
In this post, we’ll break down the key OOP concepts in Java:
- ✅ Inheritance
- ✅ Abstraction
- ✅ Encapsulation
- ✅ Polymorphism
- ✅ Class & Object
- ✅ Interface
Each concept is explained with short, clear code examples to help you apply it in real-world development.
🔹 1. Class and Object
Everything in Java starts with classes and objects. A class is a blueprint; an object is an instance.
class Car {
String brand = "Toyota";
void drive() {
System.out.println("Car is driving...");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object
System.out.println(myCar.brand);
myCar.drive();
}
}
🔹 2. Inheritance
Inheritance allows one class to inherit fields and methods from another. It helps reduce code duplication.
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // Inherited
d.bark();
}
}
🔁 Use it when multiple classes share common behavior.
🔹 3. Encapsulation
Encapsulation means hiding internal state and only exposing it through getters/setters. This protects your data and maintains control.
class BankAccount {
private double balance; // private field
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public double getBalance() {
return balance;
}
}
🔐 Make fields
private
and access them throughpublic
methods.
🔹 4. Abstraction
Abstraction lets you hide implementation details and show only the essential features. It’s done via abstract classes or interfaces.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
🧩 Use abstraction to define behavior without locking in implementation.
🔹 5. Polymorphism
Polymorphism means many forms. A subclass object can be treated as its superclass, enabling flexible and reusable code.
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Cat extends Animal {
void speak() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Cat(); // polymorphism
a.speak(); // Calls Cat's speak
}
}
📌 Useful for writing generic code using interfaces or base classes.
🔹 6. Interface
An interface defines a contract for classes. It’s a pure form of abstraction.
interface Engine {
void start();
}
class Car implements Engine {
public void start() {
System.out.println("Engine started");
}
}
🧠 Use interfaces for dependency injection and loose coupling.
🧠 Final Thoughts
OOP is more than just terminology — it’s a way of thinking. Understanding abstraction, inheritance, encapsulation, and polymorphism allows you to build systems that are easier to test, maintain, and evolve.
Whether you’re working on monoliths or microservices, mastering OOP in Java will elevate your engineering skills.
Leave a Reply