Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Access Modifiers in Java: Private

Access modifiers in Java specify the accessibility of classes and their members. There are three access modifiers - private, protected, and public - which correspond to four access levels from most to least restricted: private, default, protected, and public. Private members can only be accessed within their own class, default within the class and package, protected additionally by subclasses, and public has the broadest accessibility to all code.

Uploaded by

dheeraj bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Access Modifiers in Java: Private

Access modifiers in Java specify the accessibility of classes and their members. There are three access modifiers - private, protected, and public - which correspond to four access levels from most to least restricted: private, default, protected, and public. Private members can only be accessed within their own class, default within the class and package, protected additionally by subclasses, and public has the broadest accessibility to all code.

Uploaded by

dheeraj bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ACCESS MODIFIERS IN JAVA

Access modifiers are used to specify the accessibility or access levels of a type (class,
interface) and its members (methods, variables and even constructors). There are three
access modifiers and four access levels in Java. The three access modifiers are are private,
protected and public. Four access levels (from most restricted to least restricted) are private,
default (no modifier), protected and public. If no access modifier is specified, it is called a
default access level.

Access levels and their accessibility can be summarized as:

Private: Same class.

Default: Same class, Same package.

Protected: Same class, Same package, Subclasses.

Public: Same class, Same package, Subclasses, Everyone.

The top level classes (classes not within another class) have only public and default access;
but for inner classes (classes within classes) have all four access levels.

Private
Private members are accessible only within the same class and also are not inherited.

Default
Default (no modifier) members can be accessed by members of the same class and
members of any class in the same package.
Default members are inherited by another class only if both parent and child are in same
package.

Protected
Protected members can be accessed by

 members of the same class

 members of any class in the same package (same as default)

 subclasses (any level of subclass hierarchy) in other packages (only through inheritance).

Protected members (static or instance) cannot be accessed from a non-subclass in another


package.

Inherited protected members in the child class cannot be accessed using a parent reference
variable (irrespective of the object it point to at runtime).

Static protected members can be accessed in subclasses through:

 object reference (Parent or Child) in subclasses

 class name

Public
Public members can be accessed from everywhere within your application either through
inheritance or through object reference.

Note that the class or packages should also be accessible to access its members. You will
not be able to access a public member from a default class from another package.

Source : http://javajee.com/access-modifiers-in-java

You might also like