Sunday, June 22, 2014

Java Access Modifiers

Access Modifiers determine whether other classes can use a particular field or invoke a particular method. Modifiers (Java Access Modifiers) fall into two categories:-
  1. Access Modifiers (public ,protected, private)

  2. Non Access Modifiers (final, abstract)

JAVA Access Modifiers: -

In Java there are following Java access modifiers:-
  • public
  • protected
  • private
  • Default
Public Access: - A class declaration with the public keyword gives all classes in the Java has access to the public class called Public java access modifiers.
Protected Access: - Protected are accessible to the subclasses of the class in which they are declared called Protected java access modifiers.
Private Access: - Only objects of the same class can access a private variable or method called Private java access modifiers.
Default Access: - In Java default/package access control is set when we don't use any of three (public, protected and private) access modifiers called Default Java Access Modifiers.

Access Levels:-

As you can see public access indicates class, package, subclass and world have access to the member.
Protected access indicate class, package and subclass have access to the member but world can't access it.
Private access indicates class have access to the member but package, subclass and world can't access it.
Default access indicates class and package have access to the member but subclass and world can't access it.

Non Access Modifiers:-

  • Final: - Final keyword means the class can't be subclassed. Or we can say that, no other class can ever extend a final class.
  • Abstract: - An Abstract class can never be instantiated.

No comments:

Post a Comment