Java.Core.Explain the concept of OOP in Java. What are its principles?

🔗 What is OOP (Object-Oriented Programming)?

Object-Oriented Programming (OOP) is a programming paradigm where software is designed by modeling real-world things as objects. These objects are instances of classes, and they contain both:

Data (fields/attributes) — what the object has
Behavior (methods/functions) — what the object can do


☕️ Why is OOP important in Java?

✅ Java is a purely object-oriented language (almost everything in Java revolves around classes and objects).
✅ OOP makes code more organized, reusable, modular, and easier to maintain.
✅ It helps to model complex systems by breaking them down into smaller, manageable objects.


🚀 The 4 Core Principles of OOP (Pillars)

PrincipleMeaningKey Idea
EncapsulationHiding internal details“Protect the data”
AbstractionHiding unnecessary details“Show only what matters”
InheritanceReusing common logic“Child gets features from parent”
PolymorphismOne interface, many forms“Same method, different behavior”

🔒 1. Encapsulation

What is it?

Encapsulation means wrapping data and methods inside a class and controlling access using private, public, etc.


Example

class BankAccount {
    private double balance;  // hidden data (private)

    public void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    public double getBalance() {
        return balance;  // controlled access via method
    }
}

balance is private — outside classes cannot touch it directly.
✅ Access is controlled via methods (deposit() and getBalance()).


🎭 2. Abstraction

What is it?

Abstraction hides complex implementation details and only exposes the essential behavior.


Example

abstract class Animal {
    abstract void makeSound();  // only says "animals make sound", no details
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Woof Woof");
    }
}

Animal abstracts the concept of making sound — we only care that animals can make sound, not how.


🧬 3. Inheritance

What is it?

Inheritance allows one class to inherit fields and methods from another class.

Example

class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Woof Woof");
    }
}

Dog automatically gets the eat() method from Animal.


🔀 4. Polymorphism

What is it?

Polymorphism means the same method name can behave differently depending on the object that calls it.


Example

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Woof Woof");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();  // Polymorphism in action
        animal.makeSound();          // "Woof Woof" (actual object type decides behavior)
    }
}

✅ Even though the reference is Animal, the actual object is Dog, so Dog’s version of makeSound() runs.


📚 Quick Summary Table

PrincipleWhat it doesBenefit
EncapsulationHides internal stateProtects data, simplifies usage
AbstractionHides implementationReduces complexity
InheritanceReuses existing codeAvoids duplication
PolymorphismEnables different behavior using the same interfaceIncreases flexibility

🏁 In Short

✅ OOP lets you model the real world with objects.
✅ Objects have data + behavior.
✅ These 4 principles make your code cleaner, reusable, and easier to maintain.

This entry was posted in Без рубрики. Bookmark the permalink.