<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nuwaa</title>
    <description>The latest articles on DEV Community by Nuwaa (@nuwanisithara).</description>
    <link>https://dev.to/nuwanisithara</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3267103%2F299b7698-5c99-4be2-85a3-8f4b686ce90a.jpeg</url>
      <title>DEV Community: Nuwaa</title>
      <link>https://dev.to/nuwanisithara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nuwanisithara"/>
    <language>en</language>
    <item>
      <title>Understanding Object-Oriented Programming (OOP) with Real-World Examples</title>
      <dc:creator>Nuwaa</dc:creator>
      <pubDate>Sun, 15 Jun 2025 18:31:38 +0000</pubDate>
      <link>https://dev.to/nuwanisithara/understanding-object-oriented-programming-oop-with-real-world-examples-mec</link>
      <guid>https://dev.to/nuwanisithara/understanding-object-oriented-programming-oop-with-real-world-examples-mec</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
If you’re new to software engineering, you’ve probably heard the term “object-oriented programming,”* or OOP. It’s a fundamental way to write code that models real-world objects and behaviors. In this blog, I’ll explain the four main principles of OOP with simple real-life examples and Java code snippets. Whether you’re just starting or need a quick refresher, this guide will help you grasp the core concepts!&lt;/p&gt;

&lt;p&gt;What is object-oriented programming (OOP)?&lt;/p&gt;

&lt;p&gt;OOP organizes your code by bundling data and functions into &lt;em&gt;objects&lt;/em&gt;. It makes your programs easier to reuse, scale, and maintain.&lt;/p&gt;

&lt;p&gt;The four pillars of OOP are,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Abstraction&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encapsulation&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Encapsulation means keeping data and methods together inside a class and restricting access to the internals.&lt;/p&gt;

&lt;p&gt;Analogy: Like a TV remote — you press buttons without seeing the internal circuits.&lt;/p&gt;

&lt;p&gt;Example in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    public void deposit(double amount) {
        if (amount &amp;gt; 0) {
            balance += amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Inheritance lets a class inherit properties and methods from another, promoting code reuse.&lt;/p&gt;

&lt;p&gt;Analogy: A Dog is an Animal — it inherits common traits but has unique features.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Animal {
    public void eat() {
        System.out.println("Eating...");
    }
}

public class Dog extends Animal {
    public void bark() {
        System.out.println("Barking...");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Polymorphism means “many forms” — methods behave differently based on the object.&lt;/p&gt;

&lt;p&gt;Analogy: The word run means different actions for a person and a program.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Animal {
    public void sound() {
        System.out.println("Some sound");
    }
}

public class Dog extends Animal {
    @Override
    public void sound() {
        System.out.println("Bark");
    }
}

public class Cat extends Animal {
    @Override
    public void sound() {
        System.out.println("Meow");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Abstraction hides complexity, showing only essential features.&lt;/p&gt;

&lt;p&gt;Analogy: Driving a car without needing to understand the engine.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class Vehicle {
    abstract void start();

    public void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car started");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Summary&lt;/p&gt;

&lt;p&gt;Encapsulation: Protect data and expose only what’s necessary.&lt;br&gt;
Inheritance: Share and extend behaviors.&lt;br&gt;
Polymorphism: Same method, different implementations.&lt;br&gt;
Abstraction: Hide complexity, show essentials.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Feel free to ask questions or share your thoughts in the comments. Stay tuned for more software engineering tips!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>oop</category>
      <category>java</category>
    </item>
  </channel>
</rss>
