May
17
2015

Access Modifier in java and Difference in protected and default with example

In this tutorial of core java we will see what are access modifier available in java and what is scope of these modifier. I will also cover some misunderstanding that most of people(earlier me too :)) carry with confidence regarding modifiers scope.

These are basically four type of access modifier in java.

  1. private
  2. default or friendly
  3. protected
  4. public

The below image shows major differences in scope of these modifier.

access modifier in java with example

access modifier in java with example

First of all let me make it clear few points .

  1. For a class only public, abstract & final are permitted. 
  2. Only nested class can be protected.
  3. A class can be only default or public. If we do not write any modifier with class name then it is understood as default. We can not write default modifier explicitly.
  4. We can not extend default class

Now what i was asking about confusion that people have about modifier is about default and protected modifier. First of all is most of people think that these modifier are for class but actally these these are for member of class. If we do not specify any modifier with a class the it is consider default but we can not write explicily default with class name.

The second confusion that people have is that when class is protected then it is available in other package to extend in other class and if a class is default then it is not available for extending in other class. But this is not true.This is applied on member variale of class not the class itself. When member of a class is protected then it is available on another class of other package through extending the class in which it is declaired. But when a member is default (i.e no modofier is written then it is considered default) then it is not avialble in other class(subclass) of ther package.

Now let see some example.

FirstClass.java

FirstClass1.java

SubClassInSamePackage.java

When we run above class will get hello printed on console.

OtherClassInSamePackage.java

When we run above class will get hello printed on console.

OtherClassInOtherPack.java

Here we will get compiler will trow an exception The type com.freakers.pack.FirstClass is not visible and FirstClass cannot be resolved to a type .

Now let try by extending FirstClass

Same exception as earlier will be given by complier.FirstClass cannot be resolved to a type

Now let see what happen when we expend a public class with protected member.

SubClassInOtherPack.java

When we will try to call show method of ForstClass1 after extending FirstClass and making object of it inside SubClassInOtherPack then compiler will give The method Show() from the type FirstClass1 is not visible.

SubClassInOtherPack.java

Here show method will be called and Hello will be printed on screen.

Conclusion


  1. We can not extend default class in another package but can extend in same package.
  2. only public, abstract & final are permitted with class name.
  3. If we do not write any modifier with class name then it is treated as default.
  4.  We can access protected member of a class in other package by extending that class.

This is all in this post. Will get back with next post very soon.

 

About the Author: devender kumar

java/j2EE developer

Comments are closed.