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 are the advantages of defining packages in java?

923


What is a treemap in java?

953


What is Recursion Function?

1128


How do you define a method?

924


Which of the classes will have more memory allocated?

913


How do generics work in java?

948


How many types of the indexof method are there for strings?

921


What is the ==?

840


What is the purpose of the return statement?

983


Write a java program to print fibonacci series?

908


Why do we use string?

970


How do you add an element to an arraylist in java?

882


What is a variable in java?

1035


Explain when we should make an instance variable private.

1042


Is null == null in java?

1035