0
0
Javaprogramming~10 mins

Data hiding in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Data hiding
Start
Define class with private variables
Create public methods (getters/setters)
Create object
Access variables only via methods
End
Data hiding means keeping variables private inside a class and accessing them only through public methods.
Execution Sample
Java
class Person {
  private String name;
  public void setName(String n) { name = n; }
  public String getName() { return name; }
}

public class Main {
  public static void main(String[] args) {
    Person p = new Person();
    p.setName("Anna");
    System.out.println(p.getName());
  }
}
This code hides the 'name' variable and accesses it only through setName and getName methods.
Execution Table
StepActionVariable/MethodValue/ResultNote
1Create objectpPerson instanceObject p created
2Call settersetName("Anna")name = "Anna"Private variable 'name' set to 'Anna'
3Call gettergetName()"Anna"Returns value of 'name'
4Print outputSystem.out.printlnAnnaOutput is 'Anna'
5Try direct accessp.nameErrorCannot access private variable directly
💡 Execution stops after printing 'Anna' and error on direct access shows data hiding.
Variable Tracker
VariableStartAfter setNameAfter getNameFinal
namenull"Anna""Anna""Anna"
Key Moments - 3 Insights
Why can't we access the variable 'name' directly from the object?
Because 'name' is declared private, it is hidden from outside the class. Only methods inside the class can access it, as shown in step 5 of the execution_table.
How do we change the value of a private variable?
We use a public setter method like setName(), which changes the private variable inside the class, as shown in step 2.
How do we read the value of a private variable?
We use a public getter method like getName(), which returns the value, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 2?
AError
Bnull
C"Anna"
D"John"
💡 Hint
Check the 'After setName' column in variable_tracker and step 2 in execution_table.
At which step does the program print the value 'Anna'?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Print output' action in execution_table.
If the variable 'name' was public, what would happen at step 5?
ADirect access allowed, no error
BError because variable is private
CSetter method called automatically
DGetter method called automatically
💡 Hint
Step 5 shows error due to private access; if public, direct access would work.
Concept Snapshot
Data hiding means making class variables private.
Access them only via public getter and setter methods.
Prevents outside code from changing variables directly.
Helps protect data and control how it changes.
Use private keyword for variables, public for methods.
Full Transcript
Data hiding in Java means keeping variables private inside a class so that they cannot be accessed directly from outside. Instead, public methods called getters and setters are used to read and change these variables. In the example, the variable 'name' is private. We create a Person object 'p'. We set the name using p.setName("Anna") which changes the private variable inside the class. Then we get the name using p.getName() which returns the value. Trying to access p.name directly causes an error because it is private. This protects the data and controls how it is accessed or changed.