Skip to content

“Protected” and “Default” JAVA Access Modifiers

November 6, 2007
tags:

I guess that these two are the most confusing and misunderstood access modifiers amongst all other JAVA access modifiers for most of the average java programmers till date . I would like to take one by one. Lets start with “default first”

Default: Default access modifier is no-modifier. i.e when you do not specify any access modifier explicitly for a method, a variable or a class ( FYI : a top-level class can only be default or public access modifiers) it gets the default access. Default access also means “package-level” access. That means a default member can be accessed only inside the same package in which the member is declared.

Protected: Protected access modifier is the a little tricky and you can say is a superset of the default access modifier. Protected members are same as the default members as far as the access in the same package is concerned. The difference is that, the protected members are also accessible to the subclasses of the class in which the member is declared which are outside the package in which the parent class is present. But these protected members are “accessible outside the package only through inheritance“. i.e you can access a protected member of a class in its subclass present in some other package directly as if the member is present in the subclass itself. But that protected member will not be accessible in the subclass outside the package by using parent class’s reference. Confused with language ? Take an example. Say there is class “Super” in package A containing a protected integer variable “protected int x” and it’s subclass “Sub” in package B. The following would be a legal statement in a class B:

System.out.println(x); // valid

Whereas following would be an illegal statement:

System.out.println(new Super().x);

// invalid, as you cannot use parent class reference to access the protected member outside the package.

Once the child gets access to the parent class’s protected member, it becomes private (or rather I would say a special private member which can be inherited by the subclasses of the subclass) member of the subclass.

I hope that clarifies the difference between the private and default access modifiers. But if you have a question still about it, please leave a comment and I’ll get back to you. :-)

thx

About these ads
38 Comments leave one →
  1. sushil kumar bagi permalink
    November 8, 2007 5:22 pm

    in java by default modifier is public or private
    like
    class A
    {
    int a;
    }
    a is private or public if we not mession before a
    plzzzzzzz tell me ..

    • May 25, 2009 5:35 am

      Classes are package protected by default and it does not make any sense to have your class private. Ans that is why you even won’t be allowed to use private access modifier for classes in java.

      I am really sorry for this late reply. I hope you would have already figured this out by now. I was a little too busy, and thus totally out of touch.

      My new destination now seems to be Ruby on Rails :)

      • khyati.... permalink
        February 1, 2011 3:50 pm

        thank u …..
        It’s really nice explanation…….

      • none permalink
        December 2, 2011 2:01 pm

        Not mentioned about private inner classes.

    • February 5, 2010 4:33 am

      its default, neither public nor private

  2. November 9, 2007 6:50 am

    By default (i.e when you are not explicitly defining any access modifier, the member get “default” access. Neither private nor public.

  3. shweta permalink
    March 25, 2009 9:28 am

    in java its neither public nor private nor protected.
    in java we have 4 scope
    private
    public
    protected this is similar to default.. only difference is the variable is accessible outside the package if the class that access that variable is a sub class of the variable that declares it

    default scope of default is with in a package that is any thing that is default
    is accessible with in a package but is private outside package

  4. aram permalink
    April 1, 2009 7:16 pm

    dear thank u alot for your explanation ,but really i have pperformed it but id does not work ,can u tell me your expalnation by example.
    i am looking forword to hear from u.
    Aram

    • Marc Wrobel permalink
      July 31, 2009 3:48 pm

      Unlike what Sanjeev Mishra said previously, classes are not public by default.

      When a class is declared with no access modifier, it can be only be seen by classes within the same package the class was declared.

      There is only two scope for classes : public (visible from everywhere) and default (package-private – visible within the package it was declared).

      Concerning the access modifiers for members, it is already explained in the comment of Shweta.

      • August 1, 2009 1:57 pm

        Hey,
        I made a mistake there. I have corrected it though. Thank you Marc :)

  5. kenneth permalink
    June 29, 2009 7:01 am

    can you give me some of example of programs of protected and default access modifiers?

  6. sachin permalink
    July 16, 2009 10:42 am

    i want to know what is the advantage of protected member. as you said it is accessible through subclass reference , i am stuck in this point bec i dont understand what is the reason behind it?

    • August 1, 2009 3:52 pm

      If there is something that you do not want the whole world to have access to and just want that it should be utilized in the same family (family stands for the package here) then you would use protected identifier. For example, let’s say you are developing a tool which you would like the other’s to extend and use as third party, and there are certain things in the tool that you like that the developers of the tool sould only have access to and not others, then probably you would keep it package protected. It’s not that its a restriction, but I would say that it is more to do with indicating that, look this is something, which is internal and by using this you would probably be tightly coupling your code with the third party. :)

      I hope that answers your question.

  7. vinodjayachandran permalink
    April 10, 2010 11:31 am

    Nicely explained.
    Good work

  8. May 11, 2010 1:33 am

    You think that You explained well but again you confused us a lot.

  9. July 4, 2010 9:36 pm

    when you don’t assign explicitly any Access modifies to either class or to data member of class or method …by default it is protected neither public nor private………….

  10. Nageswararao permalink
    September 1, 2010 5:47 am

    Excellent, your explanation is good. confusion from decades fly away

  11. September 14, 2010 11:01 am

    can we use protected data in same package but different classes in different file?

  12. maher permalink
    November 10, 2010 2:23 pm

    thank you !!

  13. Panduranga permalink
    November 22, 2010 7:43 am

    Very well explained. Thanx

  14. priya permalink
    December 30, 2010 2:25 pm

    Wat is the advantage of protected over default?

  15. Niranga permalink
    February 2, 2011 6:39 am

    as u said .can we use protected member in sub class by saying-‘ sub extends super’?

  16. Atul permalink
    October 4, 2011 8:40 pm

    Thanks for good explanation. I have this question:

    com.mypkg.transport
    public class Session {
    Manager mgr;
    public connect(){
    //estabilish trasnport
    mgr = new Manager(transport);
    }
    Manager getManager();
    }

    com.mypkg.pkg1

    public class MyClass1 {
    public MyClass1(Session s);
    public myfunction() {
    mgr = s.getManager();
    mgr.lookup(..);

    }

    com.thirdparty.pkg2
    public class ThriftPartyClass{
    how to prevent access to s.getManager in here ?
    }

  17. Aravind permalink
    December 15, 2011 4:23 am

    Thanks for your explanation dude… But, Is there any special reason to make it as special private. I din’t understand why Java developers did that way… Can you explain why they have made it the way it is …

    • Sanjeev Mishra permalink*
      December 15, 2011 5:19 am

      Well, the reason is that there are things that you only want the children to inherit and not anyone who’s not the part of the family to peek into. For example, if I am developing a system that I want to expose put to the whole world, I would not like them to really see my private things, but in case that outside world is not so outsider, i.e. they are developing something which is right in my package, then I want them to access what I have. Thats the main reason why PROTECTED is actually not just protected, the meaning is PACKAGE PROTECTED. Only accessible if u are inside the same package and inheriting me directly.

      I hope that clarifies a bit!!

  18. January 31, 2012 7:29 am

    sir i tried this,
    but i can able to access only the protected static members of the class, outside the package.
    whereas im unable to access the protected non static members of the class outside the package.

    waiting for response.,
    Pradeep S M
    Triniti Solutions

  19. viveksingh permalink
    March 26, 2012 3:33 pm

    what is java.access protection?

  20. paul permalink
    May 5, 2012 1:07 pm

    nice

  21. Anupam permalink
    May 9, 2012 5:45 pm

    Nice explanation and that clarifies doubts I had

  22. sushant permalink
    June 25, 2012 6:38 pm

    Can you explain why can’t we access the protected members of superclass in derived class in another package using the reference variable of superclass?

    Thanks in anticipation…

    • Sanjeev Mishra permalink*
      July 2, 2012 7:52 am

      Well, I guess thats what the purpose of “protected” keyword and thats how JAVA is designed. if you need to access the way you want it, i.e. “derived class in another package using the reference variable of superclass”, then you better not make it protected :)

  23. Mario Calle permalink
    December 18, 2012 6:50 am

    Greetings! Very helpful advice in this particular article! It’s the little changes which will make the biggest changes. Many thanks for sharing!

  24. jay permalink
    September 30, 2013 9:39 am

    what is deffirent between default or protected

  25. Pandian permalink
    October 2, 2013 9:19 am

    Good Job, I gave got exact behavior and difference between protected and default access modifier.

  26. srinu permalink
    April 14, 2014 8:48 am

    Hi Sir,

    Recentley i got a question from interviewer what is diff b/w protected and default protected is working as a default then why we need default ?

  27. Anadi krishna permalink
    July 23, 2014 12:25 pm

    please Write An Example using Protected modifier accessing from different Packages.

Trackbacks

  1. “Protected” and “Default” JAVA Access Modifiers « Khmer Developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 391 other followers

%d bloggers like this: