Do objects encapsulate data so that even other instances of the same class cannot access the data?

In Java,

Do objects encapsulate data so that even other instances of the same class cannot access the data? Only when the keyword "private" is used? What are access methods in Java - methods like getName ()?

thanks

+5
source share
7 answers

I am not inclined to think about it from the point of view of one object that has access to another, but rather what code has access to the data that is in the object.

Java ( #, btw) . / .

- , , : , , - - . , , :

class Parent
{
    protected int x;
}

class Child1 extends Parent
class Child2 extends Parent
class Grandchild extends Child1

Child1 Parent.x , ( ) Child1 Grandchild. , new Parent().x new Child2().x.

+7

, ( ).

, , .

getter, "" . , , , , , .

, name, getName: , .

( ) , ( ).

+2

getName() ( wheather "" ).

+1

/ 'private', / , , .

+1

, . , . [] .

, (, ) . , private private [] , private .

, ( ). , . , , getters , . , 99% . private, .

+1

" " Java - , getName()?

- getFoo() setFoo() - "" foo - JavaBeans. , , , ( ), (, ) (, PropertyVetoException, ).

GUI, JavaBeans " ". , JavaBeans .

, "" , , , . - , . OO .

0

, ?

, .

:

Sometimes you want to have variables common to all objects. This is achieved using a static modifier. Fields that have a static modifier in the declaration are called static fields or class variables.

0
source

All Articles