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


Please Help Members By Posting Answers For Below Questions

What is autoboxing in java?

609


Can list contain null in java?

627


Explain wait() method of object class ?

636


How to perform binary search in java?

574


Why is a singleton bad?

558






Is there any difference between nested classes and inner classes?

540


State the difference between creating string as new () and literal.

523


What are the pillars of java?

489


Write a code to show a static variable?

618


Is java 9 released?

526


How to change value in arraylist java?

506


What is an empty string in css?

534


Is array size fixed in java?

542


What is a layout manager and what are different types of layout managers available in java awt?

730


What are java threads?

623