JavaAccess Control (Access modifier)

There are there types of access modifiers in Java.

They are: public private and protected.

public:  A public data can be accessed by outside of the code in which it is defined.

private: A private data can not be accessed by outside of the code in which it is defined. The private data can be accessed by only member function 

protected: A protected access control is similar to the private only difference is that it has a access to their derived classes. Protected applies only when inheritance outside.

default : By default access control is public means when we have not specified access control it will be private.

Difference between public and private

Result

If you comment line obj.c = 30;// error, c can’t be accessed here. then following result will produce

Private data can not be directly accessible outside class