what is the difference between the "protected and default"
modifiers?
Answer Posted / padma
“Protected” and “Default” JAVA Access Modifiers
November 6, 2007
by Sanjeev Mishra
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.
| Is This Answer Correct ? | 7 Yes | 1 No |
Post New Answer View All Answers
What are the advantages of defining packages in java?
What is a treemap in java?
What is Recursion Function?
How do you define a method?
Which of the classes will have more memory allocated?
How do generics work in java?
How many types of the indexof method are there for strings?
What is the ==?
What is the purpose of the return statement?
Write a java program to print fibonacci series?
Why do we use string?
How do you add an element to an arraylist in java?
What is a variable in java?
Explain when we should make an instance variable private.
Is null == null in java?