what is the difference between the "protected and default"
modifiers?

Answers were Sorted based on User's Feedback



what is the difference between the "protected and default" modifiers?..

Answer / naveen

default is accessble with in package only, protectd is also
same but if any class out side the package extends the
protected class then it is also accesesible means all sub
classes(what ever the package) of protechted class can
access the protected class what ever the package

i know this answer if any one has another one or if it is
not correct answer than post a correct answer

Is This Answer Correct ?    64 Yes 7 No

what is the difference between the "protected and default" modifiers?..

Answer / ravikiran

protected access is with in the same package and subclasses
outside the package
default access is with in the same package only

Is This Answer Correct ?    38 Yes 3 No

what is the difference between the "protected and default" modifiers?..

Answer / mythili s

It's all correct but one more must-be-notified and
interesting point i would like to add here along with these
is as follows.

When a protected class is extended by any other class, the
child class (in case it belongs to any other package) can
have the inheritance of the protected parent class. What if
any third class extends that child class of the protected
parent class?
Example:
PrClass1 is a protected class in package1.

Child1 is another class, belongs to package2.

..class Child1 extends PrClass1..

Here, Child1 is a class which gets all inheritance of
PrClass1.
Now, i have another class SubChilld1 belongs to package2
itself.

..class SubChild1 extends Child1..

What happens now? what happens to the PrClass1's members
which are now encapsulated by Child1???

Here we go...

Whenever the protected class is extended by any other
class, which is not in the same package, that class's
members will become private to that child class. So, even
if that child class is extended by any other class, the
private memebers of this child class cannot be accessed by.

This is a wonderful and nice point that has to be noticed.

Thanks.

Is This Answer Correct ?    23 Yes 6 No

what is the difference between the "protected and default" modifiers?..

Answer / 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

what is the difference between the "protected and default" modifiers?..

Answer / abcd

Is it possible to mark a class as Protected?? i guess the
answer posted by Mythili is wrong.. thgh we can interpret
it using protected methods..

Is This Answer Correct ?    4 Yes 0 No

what is the difference between the "protected and default" modifiers?..

Answer / firoz

default can be accessable in same class & same package.
protected can be accessed in same class,same package and
subclass.

Is This Answer Correct ?    29 Yes 26 No

what is the difference between the "protected and default" modifiers?..

Answer / gsk

Protected:
The access specifier 'protected' can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. Members are accessible only to the class and its subclass(es). Subclass can exists within the same package or any other package.

Accessibility of method or field:
Accessible within the class: Yes
Accessible to subclass within the package: Yes
Accessible to subclass outside the package: Yes
Accessible to any other class within the package: Yes
Accessible to any other class outside the package: Yes


Default (no specifier): We can have a class or method with out specifier. Default members of a class are accessible only to the class and other classes within that package. If we do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.

Accessibility of method or field:
Accessible within the class: Yes
Accessible to subclass within the package: Yes
Accessible to subclass outside the package: No
Accessible to any other class within the package: Yes
Accessible to any other class outside the package: Yes

Is This Answer Correct ?    1 Yes 3 No

what is the difference between the "protected and default" modifiers?..

Answer / waheeb

we cannot make a class protected. however it is observed
that both default and protected modifiers exhibit same
characteristics. both are not accessible outside the
package and are accessible within the same package whether
the class is inheriting from the superclass or not.

for example:
package pack1;

public class SuperClass {
public int publicvar;

protected int protectedvar;

private int privatevar;

int defaultvar;
public void publicmethod() {
System.out.println("Public Method");
}

protected void protectedmethod() {
System.out.println("Inside Protected
Method");
}

private void privatemethod() {
System.out.println("Inside Private Method");
}

void defaultmethod(){
System.out.println("Inside Default Var");
}
}

class subclass extends SuperClass{
public static void main(String args[]){
SuperClass obj = new SuperClass();
obj.protectedvar = 10;
obj.defaultvar = 10;
obj.defaultmethod();
/*
protected and default feilds accessible in
the same
*/
}
}


class anotherClass{
public static void main(String args[]){
SuperClass obj = new SuperClass();
obj.protectedvar = 10;
obj.defaultvar = 10;

/* protected and default feilds accessible
in the same
package without extending*/

}
}

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More Core Java Interview Questions

What is the maximum size of list in java?

0 Answers  


What are batch updates. in jdbc

2 Answers   Corent Technology,


What will be the default values of all the elements of an array defined as an instance variable?

0 Answers  


What do you understand by the term polymorphism?

0 Answers  


What is meant by local variable and instance variable?

0 Answers  






What do you mean by chromounits in java8?

0 Answers  


What is linkedlist in java?

0 Answers  


How can you set an applet’s height and width as a percentage?

0 Answers  


Is java supports multiple inheritance? explain?

12 Answers   BUET, Inforica,


What is command line argument in java?

0 Answers  


What is method reference in java?

0 Answers  


What is an enumeration class?

2 Answers  


Categories