java - Access private field of another object in same class -
class person { private bankaccount account; person(bankaccount account) { this.account = account; } public person somemethod(person person) { //why accessing private field possible? bankaccount = person.account; } } please forget design. know oop specifies private objects private class. question is, why oop designed such private fields have class-level access , not object-level access?
i bit curious answer.
the satisfying answer find artemix in post here (i'm renaming aclass person class): why have class-level access modifiers instead of object-level?
the private modifier enforces encapsulation principle.
the idea 'outer world' should not make changes person internal processes because person implementation may change on time (and have change whole outer world fix differences in implementation - impossible).
when instance of person accesses internals of other person instance - can sure both instances know details of implementation of person. if logic of internal person processes changed - have change code of person.
edit: please upvote artemix' answer. i'm copy-pasting it.
Comments
Post a Comment