Want to show your appreciation? Please to my charity.

Thursday, May 10, 2012

C# to Java–Access Modifiers

This is one of the informal discussions with a group undergoing the transition from C# to Java.

Assembly vs. .Jar File

.Net code need to be packed into an assembly to be useful. Java has no concept of this. Java’s runtime code unit is class file. The closest thing in Java as to the .Net assembly is a .jar file, which is merely a zip of a logically related group of class files. Metadata about the .jar file can also be pack in the .jar file. Hence, java has no version and assembly access scope.

Access Modifiers



C# has four access modifiers: public, protected, internal and private.

protected and private modifiers are not applicable to top level types.

protected is only accessible by derived types.

The default access is internal for top level types, public for interface members, and private for everything else.

You can use both internal and protected, which gives you both access.


Java has only three access modifiers: public, protected and private. But when you don’t give an access modifier, it means package access. Package access only allow classes defined in the same package to have access.

protected and private modifiers are not applicable to top level types which is same as .Net

protected gives package access plus accessible by derived classes.

The default is always package access in Java. But bear in mind that any body can define the same package name. So the access control is merely by convention and should be used as a security measure.

No comments:

Post a Comment