Solved

protected class

Posted on 2001-06-18
2
2,921 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

18 Experts available now in Live!

Get 1:1 Help Now