“Protected” and “Default” JAVA Access Modifiers
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
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 ..
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 ๐
thank u …..
It’s really nice explanation…….
Not mentioned about private inner classes.
its default, neither public nor private
By default (i.e when you are not explicitly defining any access modifier, the member get “default” access. Neither private nor public.
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
Absolutely true ๐
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
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.
Hey,
I made a mistake there. I have corrected it though. Thank you Marc ๐
can you give me some of example of programs of protected and default access modifiers?
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?
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.
Nicely explained.
Good work
You think that You explained well but again you confused us a lot.
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………….
Excellent, your explanation is good. confusion from decades fly away
can we use protected data in same package but different classes in different file?
thank you !!
Very well explained. Thanx
Wat is the advantage of protected over default?
as u said .can we use protected member in sub class by saying-‘ sub extends super’?
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 ?
}
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 …
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!!
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
what is java.access protection?
nice
Nice explanation and that clarifies doubts I had
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…
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 ๐
Greetings! Very helpful advice in this particular article! It’s the little changes which will make the biggest changes. Many thanks for sharing!
what is deffirent between default or protected
Good Job, I gave got exact behavior and difference between protected and default access modifier.
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 ?
please Write An Example using Protected modifier accessing from different Packages.
I have an protected method m1() in Class A and a Class B extends Class A …
How can i access m1 method from main method of Class B
Class A is in package com.a and Class B is in package com.b
I can’t understand these two modifier clearly as I am using it in my computer both of them are behaving same. In subclass I can’t use protected as well as default members