Private properties and functions can be accessed or modified only in the class body.
Public members can be accessed or modified anywhere - both inside and outside the class body.
It can be understood via the following code:
Output:
There are a couple of things to notice about the
private property id
and the public properties name
and age
:
lines 12 and 13
,
for private properties - line 14
.
Person
class body (like the Main function) only public
properties can be accessed/modified - lines 21 and 22
.
line 23
the code will not execute. Because
a private member (id
in this case) cannot be accessed/modified outside
the Person
class body.