Access Specifiers Concept Simplified in Java

0
18
Spread the love
  • 24
  • 25
  • 13
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    97
    Shares

 

Access Specifiers in Java
Access Specifiers in Java

Access Specifiers in Java:

Access specification is the mechanism by which we can control the use of the objects,its members and methods.
Below are the access specifier available in Java:

  1. public
  2. private
  3. protected
  4. final
  5. private and protected
  6. abstract
  7. friendly/default

Java access control mechanism (JLS-6.6) determines the accessibility of classes, interfaces and members. The accessibility of an entity is determined by the location where it is declared and by which, if any access modifiers (private protected and public ) is present in the entities declaration.

By proper using of these, we can hide information from users. The rule of thumb is that we should make each class or member as inaccessible as possible.

For top level (non-nested) classes and interfaces there are only two possible access levels(Package –Private and public). If we declare a top level class or interface with the public Modifier, it will be public.Otherwise it will be package private.

If a package private top level class or interface is used only within a single class, we need to consider making it a private nested class( or interface) of the class in which it is used. This further reduces it’s accessibility,It is however not as important to do this as it is to make an unnecessary public class package private because a package private class is already part of the package’s implementation rather than APIs.

public Access Specifier:

public classes,methods can be accessed from any other classes,methods.The public access specifier is optional and automatic.Mostly customer facing methods of a class are public in nature so that they can accept calls from other classes.So an element with public access specifier has the widest possible visibility and accessibility.

private Access Specifier: 

Elements with private access specifiers are having highest degree of protection. private elements,methods can only be accessed from that class where it is defined.A private element or method is not visible to any other class or it’s subclass.Also a subclass can not override a non private method and make a new private method.This is useful when we want to exclude classes that should not have access to the classes being defined in other package.We need to provide public accessor or public mutator method(setter and getter) in order to access private variables from other class in different package.in a simple word, the private access modifier specifies that no member except that particular class(inside methods of that class). Other classes in the same package can not access private members. A protected element is same as final.
private allows developers to work on some data members without affecting the user’s code.It is also very important in terms of multi-threading coding. Any method for that class which acts as a ‘helper’can be made private to ensure that it is not used in the package or anywhere else thus prohibits us from changing or removing the method.Private methods are the helper function in java.
A class member declared as private will remain private to its class.It is not accessible by any class outside its class including its subclass.

Package private

The member is accessible from any class in the package where it is declared. Technically known as default access. This is the access level we get if no access modifier is specified.

protected Access Specifier:

protected elements are between public and default/friendly or Protected is sort of private.The protected elements are visible not only to all classes and subclasses in the same package but also the subclasses in other package.It deals with mainly inheritance which takes an existing class and adds new members to another class without touching the existing class(base class). We can change the methods of existing class. To inherit from an existing class we need to extend that class.protected elements can be used only inside the class or it’s subclass.An inherited class can access a protected members as well as public members.A subclass can override a protected method and variable.Non-subclasses in the other packages can not access the protected elements.


class ClassA extends ClassB{
 

That is if a class inherits another class of another package, the only members we have access are public members of the base class(if we are inheriting class of the same package-we will have access to friendly/default members) and all the protected members of the class.If the creator of the base class wants to give access to some of its members to the derived classes as but not to the whole world. then protected modifier is the best suitable.A protected method has friendly access within the same package.

public and protected can not be applied to a class.As they does not make any sense. A class can not be private as it would make it accessible to no one but the class itself.If we want not to have anyone access to the class,we can make all the constructor private, that prevents anyone but us,inside a static member of the class from creating an object of that class.

class ClassA{
private ClassA()
{
}
static ClassA makeObjectA()
{
return new ClassA();
}
}
public class MyClass{
public static void main(String args[])
{
ClassA ca=new ClassA();
//not possible to call the constructor as it is private
ClassA ca=ClassA.makeObjectA();
//returns the object of the class ClassA.This is how we create an object,preventing anyone from directly accessing a particular constructor or constructors.
}

}

final Access Specifier:

All final elements of a class can not be overridden by subclasses of that class. classes can also be declared as final which denotes that the same class can not be subclassed. Please refer final keyword here.

private protected Access Specifier:

A field can be declared with two keywords private and protected together like:


private protected int codeName;

This gives visibility level in between protected and private access. The specifier makes the field visible in all sub classes regardless of what package they are in. These fields are not accessible by other classes in the same package.

abstract Access Specifier:

if anybody wants to define the method in a subclass, he can choose to use abstract keyword to mark it. In case of method , the method has to be defined in subclass. In case of class it signifies that the class is not fully implemented and it should not be instantiated.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 25
  • 13
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    97
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here