Solved

protected class

Posted on 2001-06-18
2
2,922 Views
Last Modified: 2013-11-23
Why can't the class in java be protected ?
Please explain in detail.
0
Comment
Question by:xzyeena
2 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 25 total points
ID: 6203923
Hi xzyeena...
Because it doesn't make sense. protected features (methods, attributes and nested class) of a class are accessible by the class itself, class within the same package and classes that extends the class. The difference between package and protected lies in the accesibily of the subclass to the features of the superclass. Then, if you have a class with protected accessibility will it make any difference compared to package? I hope this answers your question.

By the way, "Why can't the class in java be protected?" should be rephrased as "Why can't the TOP-LEVEL class in java be protected?"  because nested classes can be protected.

best regards...
Jerson

0
 
LVL 2

Expert Comment

by:xxMariusxx
ID: 6204826
You either want things to be visible to the outside world, or you don't.  If everyone should be able to see something, you make it public.  If you only want members of your class to see something, you make it private.

However, if you make something private, and then you subclass later on, your subclass won't have access to the private members of its superclass.  So in order to make something for all intents and purposes 'private', yet still visible to subclasses that extend your class, you make it 'protected'.

Doesn't make sense to create an entire class that the outside world cannot see (private)...and even less sense to say, "I'll hide it from the outside world...but in case they wanna subclass it...."  ...if you can't see it to begin with, you can't subclass it.

So classes are public or package friendly (no visibility modifier).  As Jerson said, though, you're free to make inner classes however you like.

Marius
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now