Bird
Raised Fist0
Interview Prepoop-design-patternseasyAmazonGoogleMicrosoftTCSInfosysWiproFlipkart

Encapsulation - Data Hiding, Getters/Setters & Access Modifiers

Choose your preparation mode3 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Define Class Person with Private Fields

The class 'Person' is defined with two private fields: '_name' of type String and '_age' of type int. These fields are not accessible outside the class directly.

💡 Private fields prevent external code from directly accessing or modifying sensitive data, enforcing encapsulation.
Line:class Person: def __init__(self): self._name = None self._age = None
💡 Data hiding is implemented by marking fields as private (underscore prefix in Python).
📊
Encapsulation - Data Hiding, Getters/Setters & Access Modifiers - Watch the Algorithm Execute, Step by Step
Watching this step-by-step helps you understand how encapsulation protects data and how getters/setters provide controlled access, which is hard to grasp by just reading code.
Step 1/11
·Active fillAnswer cell
Private fields enforce data hiding.
Person
_name: String
_age: int
Getter method added for controlled access.
Person
_name: String
_age: int
+getName()
Setter method added for controlled modification.
Person
_name: String
_age: int
+getName()
+setName()
Getter method added for '_age'.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
Setter method added for '_age'.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more
Instance created to demonstrate encapsulation in action.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more
Setter method used to update private field.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more
Setter method used to update private field.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more
Getter method used to access private field.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more
Getter method used to access private field.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more
Encapsulation pattern demonstrated fully.
Person
_name: String
_age: int
+getName()
+setName()
+getAge()
+1 more

Key Takeaways

Encapsulation hides internal data by making fields private and exposes controlled access via public getters and setters.

Reading code alone may not reveal how data hiding protects integrity; watching the step-by-step shows how access is controlled.

Getters and setters provide a safe interface to read and modify private data, allowing validation or logic to be added if needed.

Seeing the methods added and used clarifies their role beyond just simple data access.

Creating an instance and using getters/setters demonstrates encapsulation in action, showing how objects protect their own state.

Understanding encapsulation requires seeing both the class design and how instances interact with it.

Practice

(1/5)
1. In a large software system, when would applying the Interface Segregation Principle (ISP) be most beneficial?
easy
A. When clients depend on interfaces that contain methods they do not use, causing unnecessary implementation burden.
B. When all clients require the exact same set of methods, so a single fat interface simplifies design.
C. When you want to enforce a strict inheritance hierarchy with minimal interfaces.
D. When you want to reduce the number of interfaces to simplify the codebase.

Solution

  1. Step 1: Understand ISP's goal

    ISP aims to prevent clients from depending on methods they don't use, avoiding fat interfaces that force unnecessary implementations.
  2. Step 2: Analyze options

    When clients depend on interfaces that contain methods they do not use, causing unnecessary implementation burden. correctly identifies the scenario where ISP helps by splitting fat interfaces. When all clients require the exact same set of methods, so a single fat interface simplifies design. describes a scenario where ISP is less needed. When you want to enforce a strict inheritance hierarchy with minimal interfaces. confuses inheritance hierarchy with interface segregation. When you want to reduce the number of interfaces to simplify the codebase. incorrectly assumes fewer interfaces always simplify design, ignoring interface misuse.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    ISP is about splitting interfaces to avoid forcing clients to implement unused methods.
Hint: ISP splits interfaces so clients only depend on what they use.
Common Mistakes:
  • Believing fewer interfaces always mean better design
  • Thinking ISP applies when all clients use all methods
  • Confusing ISP with inheritance hierarchy rules
2. Trace the sequence of events when a client requests a service via Dependency Injection (DI) in an IoC container. Which step correctly follows the previous?
easy
A. Client creates the service instance directly, then passes it to the IoC container.
B. Client requests the service from the IoC container, which then creates and injects dependencies into the client.
C. Service creates the client instance and injects itself into the client.
D. IoC container instantiates the service and injects it into the client before the client uses it.

Solution

  1. Step 1: Understand DI and IoC flow

    In Dependency Injection via IoC, the client requests a service from the container, which manages creation and injection.
  2. Step 2: Analyze options

    Client requests the service from the IoC container, which then creates and injects dependencies into the client. correctly describes the client requesting the service and the container creating and injecting dependencies. Client creates the service instance directly, then passes it to the IoC container. reverses roles incorrectly. IoC container instantiates the service and injects it into the client before the client uses it. suggests container injects before client requests, which is inaccurate. Service creates the client instance and injects itself into the client. incorrectly states the service creates the client.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    IoC container controls creation; client depends on container to provide dependencies.
Hint: In DI, client asks container; container creates and injects dependencies.
Common Mistakes:
  • Thinking client creates service instances directly
  • Assuming container injects dependencies before client requests
  • Confusing who controls object creation
3. What is a common trade-off or limitation when applying Dependency Inversion Principle (DIP) with heavy use of Dependency Injection frameworks?
medium
A. It always improves runtime performance by reducing object creation overhead.
B. It eliminates the need for interfaces or abstractions entirely.
C. It can increase complexity and reduce code readability due to indirect dependencies and configuration.
D. It guarantees compile-time safety without any runtime errors.

Solution

  1. Step 1: Understand DIP and DI trade-offs

    While DIP and DI improve modularity, heavy use of DI frameworks can add complexity and obscure dependencies.
  2. Step 2: Analyze options

    It can increase complexity and reduce code readability due to indirect dependencies and configuration. correctly identifies increased complexity and reduced readability as a trade-off. It always improves runtime performance by reducing object creation overhead. is false; DI can add runtime overhead. It eliminates the need for interfaces or abstractions entirely. is wrong; DIP requires abstractions. It guarantees compile-time safety without any runtime errors. is incorrect; runtime errors can still occur due to misconfiguration.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    DI frameworks improve flexibility but can complicate understanding and debugging.
Hint: DI improves modularity but can complicate code and configs.
Common Mistakes:
  • Assuming DI always improves performance
  • Believing DIP removes need for interfaces
  • Thinking DI guarantees no runtime errors
4. Which of the following statements about the Single Responsibility Principle is INCORRECT?
medium
A. SRP means a class should only have one method to ensure simplicity.
B. Applying SRP improves cohesion and reduces coupling.
C. A class should have only one reason to change, which means it should have only one responsibility.
D. Violating SRP can lead to fragile code that breaks when unrelated changes occur.

Solution

  1. Step 1: Analyze each statement

    A class should have only one reason to change, which means it should have only one responsibility. correctly states the core SRP definition.
  2. Step 2: Evaluate SRP means a class should only have one method to ensure simplicity.

    SRP is about reasons to change, not the number of methods; a class can have many methods if they serve one responsibility.
  3. Step 3: Confirm options A, B, and D

    Options A, B, and D are true: SRP improves cohesion, reduces coupling, and prevents fragile code.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    SRP ≠ one method per class; it's about one reason to change.
Hint: SRP is about reasons to change, not method count.
Common Mistakes:
  • Confusing responsibility with method count.
  • Assuming fewer methods always means better design.
  • Ignoring cohesion and coupling effects.
5. What is the time complexity of calling the prepare_recipe method in the Template Method Pattern implementation for a beverage, assuming each step runs in constant time?
medium
A. O(1), since the number of steps is fixed and each step runs in constant time
B. O(log n), due to the hook method optimizing optional steps
C. O(n^2), because each step may call other steps recursively
D. O(n), where n is the number of steps in the recipe

Solution

  1. Step 1: Identify number of steps

    The template method defines a fixed sequence of steps (boil_water, brew, pour_in_cup, add_condiments).
  2. Step 2: Analyze step execution time

    Each step runs in constant time; the hook method only conditionally calls add_condiments but does not affect asymptotic complexity.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Fixed steps with constant time each -> O(1) total [OK]
Hint: Fixed step count -> constant time complexity [OK]
Common Mistakes:
  • Confusing n as input size
  • Assuming recursion adds complexity