Monday, June 1, 2015

Java for C# developers: Access Modifiers

Access Modifiers

(Java for C# developers)


What is an access modifier?

An access modifier is a keyword, which specifies the visibility and the access to a member (field, method, property) or a class. Therefore, it defines the rules by which your API/classes can be used and utilized - what can be changed, what should never change, what is internal and should not be exposed to anyone but the maintainer of the library, etc.

Why is it important?

When exposing your application to the world (i.e. - your colleagues, your clients) you must take care of protecting your classes and make them more robust. Here are some common cases, where taking a wrong decision can hurt you in the long term:

  • If a method is public everyone will start using it. This means, that it's harder for you to later hide this method. You cannot change what it's doing and you have to supported it happily ever after. (Note: there are ways to start deprecating methods, but it takes time and effort for everyone)
  • If a variable is public everyone has access to it and can easily modify it. This can change the behavior and correctness of your application.
  • If a variable is protected all inheriting classes will have access to it. This means, you have to keep it in the base class, but gives you the availability to provide special access to classes inheriting the base class.

Comparison

In C# libraries are organized in namespaces and DLLs, whereas in Java they are organized in packages. We can assume that libraries/namespaces and packages are similar and compare the access modifier in this context.

Access modifiers are slightly different on classes and class member levels, so we'll compare them separately.

Class

Access C# Java
default Defaults to internal - access limited to current assembly Defaults to package-private - access limited to the package
private Access limited only to containing class. Used for nested classes. Access limited only to containing class. Used for nested classes.
public No restriction on the access. No restriction on the access.

Member
Access C# Java
default Defaults to private - access limited to current class Visible across the package, cannot be access from inheriting classes.
private Access limited to only current class. Access limited to only current class.
public No restriction on the access. No restriction on the access.
protected Visible in current and inheriting classes. Visible in current and inheriting classes.
internal Access limited to the current assembly. No such keyword. Similar to no modifier - i.e. package-private.


As you can see, C# and Java modifiers are not so different. They are almost equivalent and the same concepts are applied in both languages. Most notable difference is that the default member access is private in C# and package-private in Java, so if you're used to not writing access modifiers, you'd better start explicitly adding them to prevent code problems and protect your variables.

Rule of thumb

Set the most specific and limited access possible - don't expose more from your classes than you need to.

The rule of thumb in access modifiers is shared amongst all OOP languages. Expose just as much as required and protect everything else. This can give you the freedom to change the underlying implementation, data structures and rename fields/methods which are not visible. Don't allow easy break-in your functionality.

References:

2 comments:

Steve Hawks said...
This comment has been removed by a blog administrator.
Mervin Parmar said...
This comment has been removed by a blog administrator.