Abstraction, Encapsulation, Inheritance, and Polymorphism in Java
Let’s cover the 4 Pillars of Object-Oriented Programming (OOP) in Java. These are fundamental concepts every Java developer must understand deeply, especially for interviews, design patterns, and building scalable systems.
π§± 1. Abstraction
β Definition:
Abstraction is the process of hiding internal implementation and showing only the necessary details to the user.
Think of it like driving a car β you use the steering wheel and pedals without needing to understand how the engine works.
π§ Why it’s important:
Defines a contract without worrying about implementation.
Promotes interface-based programming.
Decouples high-level logic from low-level implementations.
β Java Implementation:
In Java, abstraction is achieved using:
- Abstract classes
- Interfaces
π§ Example: Using an Interface
interface Animal {
void makeSound(); // abstract method
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
class Cat implements Animal {
public void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // Abstraction in action
a.makeSound(); // Output: Dog barks
}
}
π 2. Encapsulation
β Definition:
Encapsulation is the process of binding data (fields) and methods (behaviors) into a single unit and restricting direct access to some components.
This is typically done using:
- Private fields
- Public getters/setters
π§ Why it’s important:
- Improves security
- Ensures control over data
- Makes code easier to maintain
π§ Example:
public class BankAccount {
private double balance; // can't be accessed directly
public void deposit(double amount) {
if(amount > 0)
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(500);
System.out.println("Balance: " + acc.getBalance());
}
}
π¨βπ©βπ§ 3. Inheritance
β Definition:
Inheritance allows one class to inherit properties and methods from another class. It promotes code reuse and enables hierarchical classification.
extends
keyword is used for class inheritance.
π§ Why it’s important:
- Promotes DRY (Don’t Repeat Yourself)
- Simplifies code structure
- Helps model real-world hierarchies
π§ Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited method
d.bark(); // Own method
}
}
π 4. Polymorphism
β Definition:
Polymorphism means “many forms”. In Java, it allows one interface to be used for a general class of actions, with specific behavior depending on the object.
Types:
- Compile-time polymorphism (method overloading)
- Runtime polymorphism (method overriding)
π§ Example of Runtime Polymorphism:
class Animal {
void makeSound() {
System.out.println("Some sound...");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.makeSound(); // Output: Dog barks
a = new Cat();
a.makeSound(); // Output: Cat meows
}
}
π Summary Table
Concept | Meaning | Achieved Using |
---|---|---|
Abstraction | Hides complex details; shows essentials | Abstract class / Interface |
Encapsulation | Hides data using access modifiers | Private fields + Getters/Setters |
Inheritance | One class inherits from another | extends keyword |
Polymorphism | One name, many forms | Overriding / Overloading |
π Homework for Students
Create an abstract class
Shape
with an abstract methodarea()
.- Implement
Rectangle
andCircle
classes. - Each should compute and print its area.
- Implement
Create a class
Employee
with private fieldsname
,salary
.- Provide getter/setter methods.
- Create two objects and display their data.
Implement runtime polymorphism using a base class
Vehicle
and two subclassesBike
andCar
.- Call the overridden method using a parent reference.