Encapsulation
easy 35 minPut the data and the methods that work on it into one unit, and let outside code reach the data only through those methods.
Watch lesson4:51
Check yourself
One question at a time. Each answer explains why.
Question 1 of 13
What is the main purpose of encapsulation?
Which Java access level is the most restrictive?
What does a getter without a setter give?
Why check inside the method that changes the value?
In UML, what does a minus before a member mean?
What do private fields with methods give you over public fields?
`can_withdraw` and `withdraw` each have their own copy of the rule. What will happen?
A cash machine must verify a PIN. Which member fits?
The balance becomes a sum of ledger entries. Which caller breaks?
Which class needs no encapsulation?
In Java, may `equals` read `other.balance` when it is private?
Who can be stopped by `private`?
First step to remove `setBalance` with 200 callers?
Pick an answer to see why.
Not quite. Data sits with its methods and other code cannot reach around them.
Not quite. Only the class itself can name a private member.
Not quite. Callers see the value and cannot replace it.
Not quite. The check is written once and nobody can go around it.
Not quite. `+` public, `#` protected, `~` package, `-` private.
Not quite. The methods are the promise. The storage is not.
Not quite. Two copies of a rule change at different times. Share one private method.
Not quite. The question needs a yes or no. The secret never leaves.
Not quite. Protected members are public to subclasses. The field is gone.
Not quite. Without a rule between the values there is nothing to protect.
Not quite. Java privacy is per class and not per object.
Not quite. It is a tool for design. Security borders need another process and rights.
Not quite. The rules run for everybody, and you learn who would break.
Flashcards
Say your answer, then reveal it.
Card 1 of 14
Encapsulation in one sentence?
Data with its methods in one unit, reached only through a small public interface.
Say your answer, then reveal it.
Two halves of encapsulation?
Bundling and hiding.
Say your answer, then reveal it.
What is hiding for?
So that no other code depends on a decision, and it can change.
Say your answer, then reveal it.
Java levels, narrow to wide?
`private`, package (none), `protected`, `public`.
Say your answer, then reveal it.
UML signs?
`-` private, `~` package, `#` protected, `+` public.
Say your answer, then reveal it.
Why start private?
Opening later breaks nobody. Closing later does.
Say your answer, then reveal it.
Setter or action?
Action: `withdraw(200)` knows why the value changes, `setBalance` does not.
Say your answer, then reveal it.
Getter for a PIN?
None. `matches(pin)` answers yes or no.
Say your answer, then reveal it.
Encapsulation or abstraction?
Abstraction chooses the view. Encapsulation builds the wall.
Say your answer, then reveal it.
Proof that it works?
Swap the inside, run the same callers and tests.
Say your answer, then reveal it.
Private to what in Java?
To the class: two objects of one class see each other's fields.
Say your answer, then reveal it.
When is a plain record better?
When there is no rule between the values.
Say your answer, then reveal it.
Is private security?
No. It stops mistakes. Another process with rights is a border.
Say your answer, then reveal it.
Why may Python start with a public attribute?
It can become a property later, with the same caller code.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.