๐Ÿ“…Day 2 - Real-World Examples of OOP Concepts: Understanding Abstraction and Encapsulation ๐Ÿ”ฅ๐Ÿ‘ฉโ€๐Ÿ’ป

Payal KumariPayal Kumari
11 min read

NOTE: - My 8-week System Design journey with Coder Army. I'm journaling each day to capture what I learn, reflect on it, and share it with my network to help others who are new to system design.

๐Ÿ’  What is Programming Language Evolution?

Programming language evolution refers to how software languages have developed over timeโ€”from machine-level binary codes to modern object-oriented structuresโ€”to better model real-world problems, improve developer productivity, and reduce errors.

(Hindi: Programming language evolution ka matlab hai software bhashao ka vikas, jismein machine level binary code se lekar modern object-oriented languages tak ka safar shamil hai. Isse developers ko complex problems solve karne mein aasani hoti hai aur error bhi kam hoti hai.)


1. Machine Language (Binary: 0s and 1s)

In the earliest days, programmers wrote code using binary digits (0 and 1). These were direct CPU instructions.

๐Ÿ“Œ Example:
0100101
1100101

๐Ÿ“Œ Drawbacks:

  • Extremely error-prone โ€“ One wrong bit and the whole program breaks.
    (Hindi: Bahut zyada galti hone ki sambhavana โ€“ ek bit ki galti se pura program kharab ho jata tha.)

  • Tedious to write and maintain.
    (Hindi: Likhna aur maintain karna mushkil tha.)

  • No abstraction โ€“ Programmer had to manage every single detail.
    (Hindi: Koi abstraction nahi thi โ€“ har chhoti detail manually manage karni padti thi.)

๐Ÿ“Œ Real-Life Analogy:
Itโ€™s like trying to build a house by only using Morse code instructions without any blueprint.

2. Assembly Language

To simplify things, Assembly Language introduced mnemonics (like MOV, ADD, SUB). These were symbolic instructions that directly mapped to machine language.

๐Ÿ“Œ Example:
MOV A, 61h

๐Ÿ“Œ Drawbacks:

  • Still hardware-dependent โ€“ Changing CPU meant rewriting code.
    (Hindi: Ab bhi hardware ke upar depend tha โ€“ CPU badalne par code bhi badalna padta tha.)

  • No structured logic like loops or functions.
    (Hindi: Loop ya function jaise structure nahi the.)

  • Poor scalability โ€“ Only worked well for small systems.
    (Hindi: Sirf chhote system ke liye theek tha โ€“ large systems mein fail.)

๐Ÿ“Œ Real-Life Analogy:
Imagine giving cooking instructions in your native language but still needing to tell every little actionโ€”no shortcuts or reusable steps.

3. Procedural Programming (Structured Programming)

As systems grew complex, languages like C introduced structured programming: loops, functions, and conditional statements like if-else, switch.

๐Ÿ“Œ Example:
void cook() {
boilWater();
addRice();
stir();
}

๐Ÿ’  Features Introduced:

  • Functions for code reuse

    (Hindi: Code ko baar-baar use karne ke liye functions aaye.)

  • Control Structures: if-else, loops
    (Hindi: Decision making aur repetition ke liye control structures aaye jaise if-else, for loop.)

  • Better readability and debugging
    (Hindi: Code padhna aur debug karna aasaan hua.)

๐Ÿ’  Limitations:

  • Poor real-world modeling: Couldnโ€™t directly model entities like โ€œUser,โ€ โ€œDriver,โ€ or โ€œPayment.โ€
    (Hindi: Complex systems jaise ride-booking app mein user, driver jaise real-world objects ko model karna mushkil tha.)

  • No data security โ€“ Everything was globally visible.
    (Hindi: Data security ka issue tha โ€“ sab kuch globally accessible tha.)

๐Ÿ’  Real-Life Analogy:

You can cook multiple recipes using a recipe book (functions), but you still have no way to store who is the chef or assign kitchen roles (objects, permissions).

๐Ÿ“Œ Why It Matters?

Understanding these older paradigms helps us appreciate the Object-Oriented Programming (OOPs) approach that solves many of these issues โ€” like abstraction, encapsulation, and modularity โ€” enabling us to build scalable, maintainable software.

(Hindi: In purani programming styles ko samajhne se humein pata chalta hai ki Object-Oriented Programming kyun zaruri hai โ€“ yeh problems ka better solution deti hai.)


๐Ÿ’  What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a paradigm that allows us to build software by modeling it after real-world entities โ€” things that have characteristics (attributes) and can perform actions (behaviors).

(Hindi: OOP ek aisa tarika hai jisme hum real-world ki cheezon jaise objects ko code mein model karte hain. Jaise kisi cheez ke attributes (characteristics) aur behavior (karya) hote hain, waise hi hamare code mein bhi honge.)

1. Why OOP?

As software systems grew complex (think Uber, Ola, Zomato), we needed a way to map real-world problems into code more naturally and securely.

OOP allows us to:

  • ๐Ÿ‘ฅ Model real-world entities as objects (e.g., User, Car, Ride)

  • ๐Ÿ” Secure data via encapsulation (only allow safe access)

  • โ™ป Reuse code using inheritance and interfaces

  • ๐Ÿ“ˆ Build scalable systems with loosely-coupled modules

(Hindi: OOPs se hum real-world ke complex problems ko code mein asani se model kar sakte hain. Data ko secure kar sakte hain, aur code ko reuse kar sakte hain.)

๐Ÿ’  Core Ideology of OOP:

โ€œProgram the way the real world works โ€” with objects that interact.โ€

(Hindi: Programming ko is tarah karo jaise real world mein objects interact karte hain.)

Example: Car and Owner ๐Ÿš—๐Ÿง

Letโ€™s model a simple real-world interaction: A person owns and drives a car.

๐Ÿ’  Real-world Thought:

  • Object 1: Car

    • Characteristics (Attributes): brand, model, isEngineOn

    • Behaviors (Methods): start(), stop(), gearShift(), accelerate(), brake()

  • Object 2: Owner

    • Characteristics: name

    • Behavior: drive()

(Hindi: Car ek object hai jismein kuch features hain jaise brand, model, aur kuch functions hain jaise start karna, brake lagana. Waise hi owner ek object hai jo car ko use karta hai.)

๐Ÿ”ง OOP Code Representation:

๐Ÿ”น Car Class:

public class Car {
    String brand;
    String model;
    boolean isEngineOn;

    public void start() {
        isEngineOn = true;
        System.out.println("Car started");
    }

    public void stop() {
        isEngineOn = false;
        System.out.println("Car stopped");
    }

    public void gearShift() {
        System.out.println("Gear shifted");
    }

    public void accelerate() {
        System.out.println("Accelerating...");
    }
}

๐Ÿ”น Owner Class:

public class Owner {
    String name;
    Car car;

    public void drive() {
        System.out.println(name + " is driving the car.");
        car.start();
        car.gearShift();
        car.accelerate();
    }
}

๐Ÿ”น Main Function:

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.brand = "Toyota";
        myCar.model = "Corolla";

        Owner owner = new Owner();
        owner.name = "Alice";
        owner.car = myCar;

        owner.drive();
    }
}

(Hindi: Yahaan Owner aur Car ke objects ek dusre se interact kar rahe hain. Owner apni car ko drive kar raha hai, aur car ke methods jaise start(), gearShift() call ho rahe hain.)

2. Modeling Real-World Entities in Code

Letโ€™s break it down even more:

  • Object: A real-world thing (Car, User, Ride)
    (Hindi: Object matlab ek vastu jiska koi role hai, jaise car.)

  • Class: Blueprint of object (defines properties & behaviors)
    (Hindi: Class ek design hai jiske base par object banta hai.)

  • Instance: Actual object created using the class
    (Hindi: Instance class ka actual object hai jo memory mein create hota hai.)

  • Example:
    class Car โ†’ Blueprint
    Car myCar = new Car(); โ†’ Instance of Car

๐Ÿ“Œ Summary:

  • OOP makes code look and behave like the real world.

  • Every object has characteristics (fields) and behavior (methods).

  • Objects can interact with each other just like in the real world.

  • This interaction builds modular, maintainable, and scalable systems.

(Hindi: OOP mein hum aise programs banate hain jo real world ki tarah dikhte aur behave karte hain. Objects ke beech interaction ke through hum complex systems ko asani se model kar sakte hain.)


๐Ÿ’  Pillars of Object-Oriented Programming: โ€“ 1) Abstraction

  • Abstraction hides unnecessary implementation details and shows only the relevant information needed to interact with an object. Or Abstraction โ€“ "Show only whatโ€™s necessary, hide everything else!"

(Hindi: Abstraction mein kisi object ke andar ka kaam kaise ho raha hai yeh nahi dikhaya jata, sirf itna dikhaya jata hai jitna user ko chahiye. or Abstraction ka matlab hota hai โ€” "Sirf zaroori cheezein dikhana, baaki sab chhupa lena.")

  • Real-World Analogies

1 ) You can start the car, press the pedals, and turn the wheel.

What you don't need to know:
โŒ How the engine combustion works
โŒ How fuel reaches the engine
โŒ How the gears change internally

(Hindi: Aapko yeh jaanna zaroori nahi hai ki car ka engine internally kaise kaam karta hai, aapko sirf car chalani aani chahiye.)

2 ) Using a Laptop
You use apps, click buttons, browse the internetโ€ฆ

But:
โŒ You donโ€™t care how the OS schedules tasks
โŒ How the RAM and processor manage memory

Thatโ€™s abstraction โ€“ the complex logic is hidden from the user.

(Hindi: Hum laptop chalate hain, par use banane ka internal process nahi samajhte, yeh hi abstraction hai.)

  • Programming Level Abstraction

Control Structures like:
๐Ÿ”น if
๐Ÿ”น for
๐Ÿ”น while

They are high-level instructions.

The compiler takes care of converting these into machine-level instructions!

(Hindi: if-else, loops jaise keywords high-level abstraction provide karte hain. Compiler inhe machine code mein convert karta hai.)

๐Ÿ’ Code-Based Abstraction: Abstract Class

public abstract class Car {

    // Abstract methods - no implementation here
    public abstract void startEngine();
    public abstract void shiftGear(int newGear);
    public abstract void accelerate();
    public abstract void brake();
}
  • This is an abstract class.

(Hindi: Yeh ek abstract class hai jismein sirf method ke naam hain, implementation nahi.)

๐Ÿ’ Key Points in Java:

  • Java uses the abstract keyword for both abstract classes and methods.

  • Abstract methods do not have a body ({}), just a signature.

  • You cannot instantiate an abstract class directly.

  • Any class that extends Car must implement all its abstract methods, unless that class is also declared abstract.

๐Ÿ’ Concert Subclass Example

// Concrete implementation of Car
public class SportsCar extends Car {
    public void startEngine() {
        System.out.println("SportsCar engine started! ๐ŸŽ๏ธ");
    }

    public void shiftGear(int newGear) {
        System.out.println("Gear shifted to " + newGear);
    }

    public void accelerate() {
        System.out.println("SportsCar accelerating! ๐Ÿ”ฅ");
    }

    public void brake() {
        System.out.println("Applying brakes! ๐Ÿ›‘");
    }
}
public class Main {
    public static void main(String[] args) {
        Car myCar = new SportsCar();
        myCar.startEngine();
        myCar.shiftGear(2);
        myCar.accelerate();
        myCar.brake();
    }
}

๐Ÿ’  Benefits of Abstraction

  • Simplified Interfaces
    Clients focus on what an object does, not how it does it.
    (Hindi: User ko sirf object ka kaam dikhai deta hai, andar ki complexity nahi.)

  • Ease of Maintenance
    You can change the internal code without affecting the client code.
    (Hindi: Agar andar ka code badla bhi jaaye to client side pe koi asar nahi padta.)

  • Code Reuse
    You can reuse the same interface for different classes โ€“ e.g., SUV, Sedan, ElectricCar.
    (Hindi: Aap same interface se alag-alag types ke cars bana sakte ho.)

  • Reduced Complexity
    System becomes modular and easy to manage.
    (Hindi: System simple aur manageable ban jata hai.)


๐Ÿ’  Pillars of Object-Oriented Programming: โ€“ 2 ) Encapsulation

Encapsulation means wrapping data (state) and methods (behavior) into a single unit called a class and restricting direct access to some of the object's components.

Encapsulation is like a protective capsule for your data! It bundles data (variables) and behaviors (functions) together, and controls how the outside world can access them.

(Hindi: Encapsulation ek capsule ki tarah hota hai jo data aur us par hone wale operations ko ek sath bandh kar rakhta hai. Ye ensure karta hai ki sensitive data ko bahar se direct access na mile.)

๐Ÿ’  Two Facets of Encapsulation

1๏ธโƒฃ Logical Grouping

  • Data + Functions = One cohesive unit

๐Ÿ“Œ Example:
Car class will contain:

  • engineOn (bool)

  • currentSpeed (int)

  • accelerate(), shiftGear(), brake() functions

(Hindi: Logical grouping ka matlab hai related data aur functions ko ek hi jagah rakhna, jaise Car class ke andar sab kuch car se related milega.)

2๏ธโƒฃ Data Security

  • Prevent direct access to sensitive data fields.

  • Example: You can view the carโ€™s mileage (odometer) but canโ€™t reset it.

(Hindi: Data security ka matlab hai important fields ko protect karna. Jaise ki car ka odometer sirf read ho sakta hai, set nahi.)

๐Ÿ’ Real-World Analogies

๐Ÿ“Œ Medicine Capsule:

  • Data = Medicine inside

  • Wrapper = Protection

  • You can take it without knowing the inner chemicals!

(Hindi: Capsule mein dawai aur uski safety ek sath hoti hai. Aap usko lete ho bina andar ke ingredients jaane.)

๐Ÿ“Œ Car Odometer:

  • You can read kilometers driven.

  • You canโ€™t directly set it to 0.

(Hindi: Aap car ka odometer dekh sakte ho, lekin usse reset nahi kar sakte.)

public class Car {
    private int speed;
    private int odometer;

    public void setSpeed(int s) {
        if (s > 0) {
            speed = s;
        }
    }

    public int getSpeed() {
        return speed;
    }

    public int getOdometer() {
        return odometer;
    }

    public void drive(int distance) {
        if (distance > 0) {
            odometer += distance;
        }
    }
}

๐Ÿ’  Access Modifiers in Java

๐Ÿ”“ public โ€” Accessible from anywhere
๐Ÿ”’ private โ€” Only accessible inside the class
๐Ÿ” protected โ€” Accessible in the class and its child classes

(Hindi: Ye access modifiers define karte hain ki kaunsa data kahaan se access ho sakta hai.)

๐Ÿ’ Getters & Setters: Controlled Access

  • Getters allow safe read access.

  • Setters allow controlled modifications with validations.

Allow controlled mutation with checks, rather then exposing fileds blindly

(Hindi: Getter aur Setter functions se aap data ko secure tarike se access aur modify kar sakte ho, bina direct access diye.)

๐Ÿ’  Benefits of Encapsulation

  • Robustness:
    Protects data from accidental or intentional misuse.

  • Maintainability:
    You can change internal code without affecting outside users.

  • Clear Contracts:
    Users interact via defined functions only.

  • Modularity:
    Helps organize code better and makes testing easier.


๐Ÿ’  Difference: Data Hiding vs Data Security

  • Data Security:
    Important info agar leak ho gaya toh threat ban sakta hai.

  • Data Hiding:
    Agar kuch data hide bhi ho gaya aur kisi ne dekh liya โ€” koi dikkat nahi.

(Hindi: Data hiding aur security mein farq hai. Security mein data leak se danger ho sakta hai. Hiding mein aisa zaroori nahi.)


Day 2 Completed โœ… System Design

NOTE : - A big thanks to my mentors Rohit Negi Sir and Aditya Sir for launching this amazing 8-week course absolutely free on YouTube via CoderArmy9 :- https://www.youtube.com/@CoderArmy9 . ๐Ÿ™Œ

๐Ÿ‘‰ Share this blog with your connections! Letโ€™s keep learning, growing, and supporting one another on this journey. ๐Ÿš€

โœ๏ธ Payal Kumari ๐Ÿ‘ฉโ€๐Ÿ’ป

Jai Hind ๐Ÿ‡ฎ๐Ÿ‡ณ | #CoderArmy #LearningInPublic #SystemDesign #TechForAll #MentorshipMatters #8weeksLLdChallenge #LowLevelDesign #Code #LLD #OOP ๐Ÿ‘ฉโ€๐Ÿ’ป

0
Subscribe to my newsletter

Read articles from Payal Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Payal Kumari
Payal Kumari

I'm a passionate full-stack developer with a strong foundation in the MERN stackโ€”building and maintaining scalable web applications using React.js, Node.js, and Next.js. My journey in open source began with Hacktoberfest 2023, where I made four impactful pull requests that sparked a love for collaborative coding, global learning, and open knowledge sharing. Since then, Iโ€™ve contributed to and mentored projects in top open source programs like GSSoCโ€™24, SSOCโ€™24, and C4GTโ€™24. As a Google Gen AI Exchange Hackathon โ€™24 Finalist and Googleโ€™s Women Techmakers (WTM) Ambassador, Iโ€™ve been privileged to support diverse communities in building meaningful tech solutions. My work as a Top 50 Mentor for GSSoC โ€™24 reflects my commitment to nurturing new talent in tech. Beyond development, I serve as a Student Career Guide, Profile Building Expert & Evangelist at Topmate.io, where I conduct workshops, guide students through resume building and career strategy, and help mentees navigate open source and tech careers. Recognized among the Top 5% of mentors and featured on โ€œTopmate Discover,โ€ I take pride in making mentorship accessible and impactful. My technical voice has also been acknowledged by LinkedIn, where Iโ€™ve earned the Top Voice badge seven times in domains like web development, programming, and software engineering. In addition, I hold LinkedIn Golden Badges for Research Skills, Interpersonal Skills, Critical Thinking, and Teamworkโ€”signaling a well-rounded approach to both individual contribution and team collaboration. Graduating with an MCA from Chandigarh University in 2023, Iโ€™ve continued to fuel my curiosity by writing technical articles and sharing practical MERN stack insights across platforms. Whether itโ€™s building polished UIs, optimizing backend performance, or guiding a mentee through their first pull request, Iโ€™m driven by the power of community and continuous learning. Letโ€™s connect! I'm open to collaborations, mentorship, or building something impactful together. Reach out to me at kumaripayal7488@gmail.com or visit my profile on Topmate.io.