Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 string pool in java?

1017


Explain about doubly linked list

1030


What is another word for methodology?

1013


Are private methods final?

901


What is class??

1229


What is the full name of java?

1114


What is variable and its types?

1010


Does string is thread-safe in java?

1083


Can we serialize singleton class?

1033


What does the string method compareto () do?

1055


Can we declare an array without size in java?

996


Is it necessary that each try block must be followed by a catch block?

1016


Define inheritance with reference to java.

1087


what is the difference between future and callable interface in java?

1230


What does singleton class mean?

971