java - What's meant by "Data Encapsulation"? Use private data fields or methods? -
say have classa
, classb
extends classa
classa { private int x; private void m1() { } } classb extends classa { }
under data encapsulation, should private data field , methods. correct?
but, in case, classb
can not use classa
's data field , methods.
so, under data encapsulation should use private access modifiers only?
can use protected access modifier rather private solve problem under rule of data encapsulation?
essentially, encapsulation way of making sure functions in class ran processes tested , known work. if function has no reason called object other ones intend to, why should given ability seen , accidentally called?
it's more form of programmer controls, setting rules , basic guidelines of proper code use, in api scenario want expose functions or classes.
"good practice" states should using 'private' functions not interface other class itself. functions need interface children classes, can use protected. other interfacing between other classes, use public.
Comments
Post a Comment