<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mastering Java OOPs Dive into Pillars]]></title><description><![CDATA[Mastering Java OOPs Dive into Pillars]]></description><link>https://oops-concepts.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 12 Nov 2026 12:43:12 GMT</lastBuildDate><atom:link href="https://oops-concepts.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Four Pillars of OOP in Java: A Quick Overview 🧱]]></title><description><![CDATA[Object-Oriented Programming (OOP) is the backbone of Java. Every class you write, every Spring bean you inject and every library you use is built on four…]]></description><link>https://oops-concepts.hashnode.dev/the-four-pillars-of-oop-in-java-a-quick-overview</link><guid isPermaLink="true">https://oops-concepts.hashnode.dev/the-four-pillars-of-oop-in-java-a-quick-overview</guid><category><![CDATA[Java]]></category><category><![CDATA[oop]]></category><category><![CDATA[Object Oriented Programming]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Sahil Maddi]]></dc:creator><pubDate>Tue, 10 Nov 2026 02:41:47 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/653b961d0f79ef82f41fd905/b2fdf403-d3d7-4902-9814-4df6248cc10e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Object-Oriented Programming (OOP) is the backbone of Java. Every class you write, every Spring bean you inject and every library you use is built on four ideas: <strong>encapsulation, inheritance, polymorphism and abstraction</strong>. This article walks through each pillar with a short example and the practical reason it matters in real projects.</p>
<h2>1. Encapsulation 🔒</h2>
<p>Encapsulation means keeping an object's data private and exposing behaviour through methods. Other code can't put the object into an invalid state because it never touches the fields directly.</p>
<pre><code class="language-java">public class BankAccount {
    private double balance;

    public void deposit(double amount) {
        if (amount &lt;= 0) {
            throw new IllegalArgumentException("Deposit must be positive");
        }
        balance += amount;
    }

    public double getBalance() {
        return balance;
    }
}
</code></pre>
<p><strong>Why it matters:</strong></p>
<ul>
<li><strong>Validation in one place:</strong> the rule "deposits must be positive" lives inside the class, not scattered across callers.</li>
<li><strong>Freedom to change internals:</strong> you can switch <code>double</code> to <code>BigDecimal</code> later without breaking code that only calls <code>deposit()</code> and <code>getBalance()</code>.</li>
<li><strong>Safer APIs:</strong> in Spring Boot, entities and DTOs with private fields and controlled setters prevent accidental changes.</li>
</ul>
<h2>2. Inheritance 🧬</h2>
<p>Inheritance lets a class reuse and extend another class using <code>extends</code>. The child class gets the parent's fields and methods and can add or override behaviour.</p>
<pre><code class="language-java">public class Employee {
    protected String name;

    public double monthlyPay() {
        return 0;
    }
}

public class FullTimeEmployee extends Employee {
    private double salary;

    @Override
    public double monthlyPay() {
        return salary / 12;
    }
}
</code></pre>
<p><strong>Why it matters:</strong></p>
<ul>
<li><strong>Reuse:</strong> shared fields and logic are written once in the parent.</li>
<li><strong>"Is-a" relationships:</strong> a <code>FullTimeEmployee</code> <em>is an</em> <code>Employee</code>, so it can be used anywhere an <code>Employee</code> is expected.</li>
</ul>
<p><strong>A word of caution:</strong> deep inheritance trees become hard to change. Modern Java code often prefers <strong>composition</strong> (a class <em>has</em> another object) over inheritance, and uses inheritance only for true "is-a" relationships.</p>
<h2>3. Polymorphism 🎭</h2>
<p>Polymorphism means "many forms": the same method call behaves differently depending on the actual object. Java supports two kinds.</p>
<p><strong>Compile-time polymorphism (method overloading):</strong> same method name, different parameters.</p>
<pre><code class="language-java">public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
</code></pre>
<p><strong>Runtime polymorphism (method overriding):</strong> a parent reference points to a child object, and Java calls the child's version at runtime.</p>
<pre><code class="language-java">List&lt;Employee&gt; team = List.of(new FullTimeEmployee(), new Contractor()); // Contractor also extends Employee
for (Employee e : team) {
    System.out.println(e.monthlyPay()); // each object uses its own monthlyPay()
}
</code></pre>
<p><strong>Why it matters:</strong></p>
<ul>
<li><strong>Code to the type, not the implementation:</strong> the loop above doesn't care what kind of employee it has.</li>
<li><strong>Easy to extend:</strong> add a new <code>Intern</code> class and the loop keeps working without changes.</li>
<li><strong>It powers Spring:</strong> when you inject a <code>PaymentService</code> interface, Spring hands you a concrete implementation at runtime. That's runtime polymorphism.</li>
</ul>
<h2>4. Abstraction 🧩</h2>
<p>Abstraction hides <em>how</em> something works and shows only <em>what</em> it does. In Java you achieve it with <strong>abstract classes</strong> and <strong>interfaces</strong>.</p>
<pre><code class="language-java">public interface NotificationService {
    void send(String to, String message);
}

public class EmailNotificationService implements NotificationService {
    @Override
    public void send(String to, String message) {
        // SMTP details stay hidden here
    }
}

public class SmsNotificationService implements NotificationService {
    @Override
    public void send(String to, String message) {
        // SMS gateway details stay hidden here
    }
}
</code></pre>
<p><strong>Interface vs abstract class, in short:</strong></p>
<ul>
<li><strong>Interface:</strong> a contract. A class can implement many interfaces. Since Java 8, interfaces can also have <code>default</code> and <code>static</code> methods.</li>
<li><strong>Abstract class:</strong> a partial implementation. It can hold state (fields) and constructors, but a class can extend only one.</li>
</ul>
<p><strong>Why it matters:</strong> callers depend on <code>NotificationService</code>, not on email or SMS details, so you can swap or add channels without touching business logic.</p>
<h2>How the four pillars work together</h2>
<p>Think of a typical Spring Boot service layer:</p>
<ul>
<li><strong>Abstraction:</strong> controllers depend on a <code>UserService</code> interface.</li>
<li><strong>Polymorphism:</strong> Spring injects <code>UserServiceImpl</code> at runtime, and tests can inject a mock instead.</li>
<li><strong>Encapsulation:</strong> the <code>User</code> entity keeps its fields private and validates changes.</li>
<li><strong>Inheritance:</strong> a <code>BaseEntity</code> class can hold shared fields like <code>id</code> and <code>createdAt</code> for every entity.</li>
</ul>
<h2>Quick recap</h2>
<table>
<thead>
<tr>
<th>Pillar</th>
<th>One-line meaning</th>
<th>Java tools</th>
</tr>
</thead>
<tbody><tr>
<td>Encapsulation</td>
<td>Protect data, expose behaviour</td>
<td><code>private</code> fields, getters and methods</td>
</tr>
<tr>
<td>Inheritance</td>
<td>Reuse and extend a parent class</td>
<td><code>extends</code>, <code>super</code>, <code>@Override</code></td>
</tr>
<tr>
<td>Polymorphism</td>
<td>One call, many behaviours</td>
<td>Overloading, overriding, interfaces</td>
</tr>
<tr>
<td>Abstraction</td>
<td>Show what, hide how</td>
<td><code>interface</code>, <code>abstract class</code></td>
</tr>
</tbody></table>
<h2>Conclusion</h2>
<p>The four pillars aren't just interview theory. They decide how easy your code is to test, extend and maintain. Encapsulation keeps objects valid, inheritance reuses shared behaviour, polymorphism lets one piece of code handle many types, and abstraction lets you swap implementations without breaking callers. Master these, and frameworks like Spring Boot start to feel natural, because they're built on exactly these ideas. 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Mastering Java OOPs: Dive into the Pillars of Object-Oriented Programming]]></title><description><![CDATA[Introduction: Start with a hook, like: "Java has earned its spot as one of the most popular programming languages, thanks in no small part to its robust…]]></description><link>https://oops-concepts.hashnode.dev/mastering-java-oops-dive-into-the-pillars-of-object-oriented-programming</link><guid isPermaLink="true">https://oops-concepts.hashnode.dev/mastering-java-oops-dive-into-the-pillars-of-object-oriented-programming</guid><dc:creator><![CDATA[Sahil Maddi]]></dc:creator><pubDate>Sat, 15 Mar 2025 16:57:41 GMT</pubDate><content:encoded><![CDATA[<hr />
<p>Introduction: Start with a hook, like: "Java has earned its spot as one of the most popular programming languages, thanks in no small part to its robust implementation of Object-Oriented Programming principles. Whether you're a beginner or brushing up on your skills, understanding Java OOPs can transform the way you approach coding!" Introduce what OOP is, and why Java is closely tied to it.</p>
<hr />
<p><strong>Body</strong>:</p>
<ol>
<li><p><strong>What is Object-Oriented Programming (OOP)?</strong></p>
<ul>
<li><p>Define OOP and its purpose: creating programs that model real-world entities.</p>
</li>
<li><p>Why is OOP so important in Java?</p>
</li>
</ul>
</li>
<li><p><strong>The Four Pillars of OOP in Java</strong>:</p>
<ul>
<li><p><strong>Encapsulation</strong>: Explain the concept with an example of using private variables and getters/setters in a class.</p>
</li>
<li><p><strong>Inheritance</strong>: Describe how classes can inherit behaviors and properties from other classes, with a simple <code>Animal</code> → <code>Dog</code> example.</p>
</li>
<li><p><strong>Polymorphism</strong>: Demonstrate method overriding and overloading with concise examples.</p>
</li>
<li><p><strong>Abstraction</strong>: Highlight abstract classes and interfaces in Java, showcasing their use cases.</p>
</li>
</ul>
</li>
<li><p><strong>Real-World Examples in Java</strong>: Provide a simple program (e.g., a <code>Shape</code> class with subclasses like <code>Circle</code> and <code>Rectangle</code>).</p>
</li>
</ol>
<hr />
<p><strong>Conclusion</strong>: Summarize why mastering OOP is essential for Java developers. Encourage readers to try building their own OOP-based applications.</p>
<hr />
<p><strong>Pro Tip</strong>: Include snippets of well-commented code and diagrams to make the article visually appealing. Adding relatable metaphors (like thinking of objects as 'actors' in a play) can also enhance understanding.</p>
<p>If you'd like, I can help flesh out any specific sections or even draft the first couple of paragraphs! Let me know. 😊</p>
<p>Here's some Java code that demonstrates the four pillars of Object-Oriented Programming (OOP) using a simple example. Each section illustrates Encapsulation, Inheritance, Polymorphism, and Abstraction:</p>
<pre><code class="lang-plaintext">// Encapsulation: Hiding details with private fields and providing access via methods
class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter
    public String getName() {
        return name;
    }

    // Setter
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

// Inheritance: Extending the behavior of a base class
class Employee extends Person {
    private String department;

    public Employee(String name, int age, String department) {
        super(name, age); // Call to the parent class (Person) constructor
        this.department = department;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Department: " + department);
    }
}

// Polymorphism: Method Overloading and Overriding
class Calculator {
    // Method Overloading: Same method name, different parameters
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }
}

class Manager extends Employee {
    private String project;

    public Manager(String name, int age, String department, String project) {
        super(name, age, department);
        this.project = project;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Project: " + project);
    }
}

// Abstraction: Using abstract classes and interfaces
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a Circle");
    }
}

class Rectangle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing a Rectangle");
    }
}

public class OOPDemo {
    public static void main(String[] args) {
        // Encapsulation
        Person person = new Person("Alice", 30);
        person.displayInfo();

        // Inheritance &amp; Polymorphism (Method Overriding)
        Employee employee = new Employee("Bob", 25, "IT");
        employee.displayInfo();

        Manager manager = new Manager("Carol", 35, "HR", "Recruitment");
        manager.displayInfo();

        // Polymorphism (Method Overloading)
        Calculator calc = new Calculator();
        System.out.println("Integer addition: " + calc.add(2, 3));
        System.out.println("Double addition: " + calc.add(2.5, 3.5));

        // Abstraction
        Shape circle = new Circle();
        circle.draw();

        Shape rectangle = new Rectangle();
        rectangle.draw();
    }
}
</code></pre>
<p>You can compile and run this code to see how the principles of Java OOP work together! Let me know if you'd like further clarification or enhancements.</p>
]]></content:encoded></item></channel></rss>