HackIndia Logo
HackathonsNewsSkillsGrowth
HackIndia LogoContact: +91 9910125804

General

  • Companies
  • Colleges
  • Board of Advisors
  • Brand Ambassadors
  • FAQ
  • Contact
  • Report a Bug

Hackathons

  • Upcoming Hackathons
  • Judges
  • Mentors
  • Partners
  • Participating Colleges
  • Testimonials

Resources

  • About Us
  • Branding
  • Latest News
  • Growth
  • Skills
  • Builders
  • Ecosystem
  • Forum
  • Ideas
  • Prompt Optimizer
  • Games

© 2026 HackIndia. All rights reserved.

Privacy PolicyTerms of ServiceCode of ConductSitemap

Build 2.139 · 05 Nov 2026, 10:27 UTC

    HackIndia
    HackathonsNewsInternshipsSkillsGrowthPro Perks
    Working towardsOOP
    0%

    0 of 9 steps done · +100 XP · +50 SHARP when it is yours

    1. 1Objects that solve features4 min · +10 XP · +5 SHARP
    2. 2Classes, objects, and state4 min · +10 XP · +5 SHARP
    3. 3Encapsulation without overengineering4 min · +10 XP · +5 SHARP
    4. 4Composition before inheritance4 min · +10 XP · +5 SHARP
    5. 5Polymorphism through interfaces5 min · +10 XP · +5 SHARP
    6. 6SOLID in plain words4 min · +10 XP · +5 SHARP
    7. 7Build and submit your repo5 min · +10 XP · +5 SHARP
    8. The check10 questions, 7 to pass · +10 SHARP
    9. Hand in the taskA project that uses the skill
    10. Skill and certificate+100 XP · +50 SHARP
    Lesson 3 of 7 4 min

    OOP

    Encapsulation without overengineering

    • +10 XP for this lesson
    • +100 XP when the skill is proved
    • Nobody here has proved it yet
    In 30 seconds
    • Hide the data that should not be changed directly.
    • Expose small methods that match the feature’s real actions.
    Avoid getters, setters, and layers that do not protect anything.
    Key point

    One idea

    Encapsulation means your object protects its own rules. Other code should ask the object to do a valid action, not reach inside and edit its state.

    Why it matters

    In a hackathon repo, recruiters do not expect a huge design. They look for code where bugs are harder to create.

    If anyone can change every field, your class is just a loose data bag. A small method like markPaid() or assignDriver() can check rules before state changes.

    Example

    Bad shape

    This looks simple, but any part of the app can put the order into a wrong state. You can mark it paid with zero amount. You can set any random status string.

    class FoodOrder:
        def __init__(self, order_id, total_rupees):
            self.order_id = order_id
            self.total_rupees = total_rupees
            self.status = "CREATED"
            self.paid = False
    
    order = FoodOrder("ORD-17", 0)
    order.paid = True
    order.status = "DONE_DONE"
    Example

    Better shape

    Keep fields private by convention, then expose methods for real actions. This version is still small. No framework. No extra pattern.

    class FoodOrder:
        VALID_STATUSES = {"CREATED", "PAID", "CANCELLED"}
    
        def __init__(self, order_id, total_rupees):
            if total_rupees <= 0:
                raise ValueError("Order total must be more than 0")
            self._order_id = order_id
            self._total_rupees = total_rupees
            self._status = "CREATED"
    
        def mark_paid(self):
            if self._status == "CANCELLED":
                raise ValueError("Cancelled order cannot be paid")
            self._status = "PAID"
    
        def cancel(self):
            if self._status == "PAID":
                raise ValueError("Paid order cannot be cancelled here")
            self._status = "CANCELLED"
    
        def summary(self):
            return {
                "order_id": self._order_id,
                "total_rupees": self._total_rupees,
                "status": self._status
            }
    
    order = FoodOrder("ORD-17", 199)
    order.mark_paid()
    print(order.summary())
    Your to-do

    Encapsulate a class

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Common mistake

    Getter setter trap

    Do not create getStatus() and setStatus() for every field by habit. That often gives public access with extra typing.

    Use a setter only when outside code is allowed to change that value directly. If there is a rule, name the method after the action and enforce the rule there.

    Tip

    Use AI safely

    You can ask ChatGPT or Gemini: “Given this class, which fields should be private and what methods should change them?” Then check the answer yourself.

    Reject suggestions that add abstract factories, managers, or service layers for a tiny feature. For this lesson, one clear class with protected rules is enough.

    Your to-do

    Do this now

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Quick check

    0 of 2 right
    1.Your Order class has status. Any file can set order.status = 'XYZ'. What is the best fix?
    2.When should you avoid adding getters and setters for every field?

    Answer the quick check to finish this lesson.

    ← Lesson 2Skip to lesson 4 →
    Working towards
    OOP +100 XP

    0 of 7 lessons done · proved by a project that uses the skill

    See the track