Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

protected class

Posted on 2001-06-18
2
Medium Priority
?
2,933 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

636 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