Access Modifiers: Public, Private, Protected, Internal

Variables

If a variable is declared outside a class method, it is a class variable. Class variables, or properties, can have access modifiers, public, private, protected, or internal.

A private variable can only be accessed from within the class itself, whereas public variables can be accessed by objects of another class.

Protected variables can be accessed from an instance of the class or an instance of any subclass, and internal variables can be accessed by any class within the same package.

If no access modifier is specified, it defaults to internal.

Methods

Like class variables, methods can be declared with access modifiers. These determine which other classes are able to call the methods. The available access modifiers are:

private

Can only be accessed from within the class itself.

protected

Can be accessed by the class or any subclass. This is instance-based. In other words, an instance of a class can access its own protected members or those of its superclasses. It cannot access protected members on other instances of the same class.

internal

Can be accessed by the class or any class within the same package.

public

Can be accessed by any class.

If you do not specify an access modifier explicitly, the method takes on the default internal access.

Post A Comment

Anti-Spam Quiz: