?
Solved

protected class

Posted on 2001-06-18
2
Medium Priority
?
2,930 Views
Last Modified: 2013-11-23
Why can't the class in java be protected ?
Please explain in detail.
0
Comment
Question by:xzyeena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 100 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

770 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