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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you empty a list in java?

545


When a byte datatype is used?

567


Explain the difference between serializable and externalizable in java?

540


Give a practical example of singleton class usage?

593


What do you mean Abstraction in java?

611






What is string data type?

555


How many ways can we create singleton class?

506


Differences between external iteration and internal iteration?

577


Why is multiple inheritance not supported in java?

561


How to retrieve data from database in java using arraylist?

530


What is the base class of all exception classes?

586


why we use merge option in hybernate pls give a ex snippet

1464


What are format specifiers in java?

598


What is static variable with example?

563


How many types of methods are there in java?

530