I have this simple code :
public class A
{
int _private=3;
public A (B b)
{
b._private=5;
}
}
public class B:A
{
}
this code compiles
-
Via
OOP– thebshould not provide access to_private. -
Via private and
A,Aknows_private, but still the access is made throughb!
what is going on here ?
The private access modifier gives access to a member within body of the type in which it is declared. It does not matter that
bis another instance. It is anAso methods declared inside ofAcan access_private. I will also note that this example is more or less straight out of the C# 4.0 spec, section 3.5.2 about accessibility domains:I think of the OOP recommendations for information hiding being about specifying the contract for client code. Public is something that is supported. Private is an implementation detail. Here, we could modify the implementation of
Aby removing_privateand client code would be unaffected.