wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class cannot be protected (or) static ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class cannot be protected (or) static ?" Watch "Class cannot be protected (or) static ?" New topic
Author

Class cannot be protected (or) static ?

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, I am wondering why a class(outer class) cannot be defined as protected and/or static ? Looking for a reason behind that.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You can define a static class within a enclosing class. And, it will behave like any other top level class. But, in the case of protected, what do you want to achieve a class with a protected modifier. Because, making a member as protected will be benefit with the use of objects.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Abimaran, I am concerned about only outer classes only. With inner classes you can define protected or static which is fine with me. Why Sun is not allowing to declare static or protected for regular classes or Outer classes. I don't know want to achieve but wondering why I am getting compiler error. Please let me know if there is a concrete reason behind that
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Two steps:

(1) Ask yourself what "protected" means.

(2) Ask yourself how that meaning would apply to a top-level class.

Okay? If that doesn't give you your answer, then tell us what you got for step (1).
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
(1)&(2)

Protected = within package accessibility + subclass(es) outside package. My requirement here is, I want to give access only to subclasses outside the package.
For example, If I say HelloProtected as public in package1, then all classes in different package(package2) can access it which I do not want.. Please let me know if I am not clear.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Saibabaa Pragada wrote:(1)&(2)

Protected = within package accessibility + subclass(es) outside package. My requirement here is, I want to give access only to subclasses outside the package.


But that isn't anything like your original question. So is that what you meant to ask?
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
I don't know if this is confusing..If I say public, all classes in different package can use it which I do not want. I want to give access only to subclasses in different package which can extends base class(protected access). For that I want to make base class as protected but I am getting compile error. In a nutshell, my question is Why base class cannot be protected or static.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Saibabaa Pragada wrote:I don't know if this is confusing.

Yes, it is.
If I say public, all classes in different package can use it which I do not want. I want to give access only to subclasses in different package which can extends base class(protected access).

This is the confusing part. First, why would you want that? You really want to not give access to subclasses in the same package? What is the use case for that? But second, and more important, what does that have to do with the SCJP certification?
For that I want to make base class as protected but I am getting compile error. In a nutshell, my question is Why base class cannot be protected or static.

Well, again, if you know what "protected" means then you should be able to figure out why you can't have a protected top-level class. But you haven't been focusing on that, you have been diverted by your peculiar requirement.

So what is it? Is this an SCJP question about the potential protectedness of classes? Or is it a general Java question about the requirement you asked about later in the thread? If it's the latter then let me know and I will move the thread to the right forum.
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi Paul, This is from SCJP mock exam Whizlabs. I gave my requirement to explain you in a better way. Because it is confusing, Please ignore that. All I understand about "protected" is Protected = within package accessibility + subclass(es) outside package. protected for variables and methods makes sense to me. But, I still do not understand why it cannot be applied to class(outer class)
Paul Clapham wrote:Well, again, if you know what "protected" means then you should be able to figure out why you can't have a protected top-level class. But you haven't been focusing on that, you have been diverted by your peculiar requirement.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay, so it's an SCJP question. Then making up fake requirements is misleading and unhelpful, especially when those fake requirements don't really make sense. That just distracts people into discussing why they don't make sense instead of focusing on the real question.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

The Java language specification states guidelines (rules?) which are to be followed. There would be some idea behind not keeping other access modifiers for Classes.

Packages- To keep the related classes together. There would be some classes which can be used by other packages so they would be Public.

So if you have a class which tried to extend a Class which is visible within the package- Then I would say that one has to check the design. As usually we keep related classes together.


Mohamed Sanaulla | My Blog
Anchit Herredia
Greenhorn

Joined: Jul 15, 2009
Posts: 26
Out of the 4 access modifiers "public, private, protected and default <no modifier>" a class can have only public and default/no modifier.

If you have a public class you can go about using it wherever you want which is very simple. You can import it in any package and start using it.

But, if you have a class with no modifier/default then you cannot even import it in another package. For example you have the class:

DefaultClass.java


TestingIt.java


The moment you try to import home.DefaultClass in the above code you will realize that our DefaultClass cannot be imported. It is not visible to a package outside home. We cannot import it in this TestingIt.java file. Why not? because default = limited to its own package.

And now coming to your question "why cant a class have protected access-modifier?"
I think its probably because it would be no different than a default/no modifier class. Even if "protected class" was possible you would not be able to import it in another package just like a "default/no modifier class".

You could use them in the same package, but once you import them both would function exactly same in the same package. Hence, as far as access modifiers for classes are concerned both protected and default are the same.

What do you think?
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

As anchit said , protected and default modifiers are almost the same outside the package except for protected being accessible by the sub-classes in other package ....But i guess reason behind protected being not allowed as modifier for outer classes is evident .... Java allows the protected modifier for variables and methods ,so instead of declaring the entire class as protected ,you could only declare those methods which you want to be protected ...this avoids compiler to check the entire imported class each time and when you instantiate the present class each time.


"Known is a drop , unknown is an ocean " !
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

Anchit Here wrote:If you have a public class you can go about using it wherever you want which is very simple.


I would like to go a little bit off topic here, and say that while public classes may be easy to use, they are hard to maintain. Strive to only make classes public if they really have to be. Always prefer default access.
samir vasani
Ranch Hand

Joined: Nov 24, 2010
Posts: 59
Hi,

In reall life your requirement doesnt mean to do anything.
And its Sun's convention to use only public and default modifier with the class.So Sun has not provided to use static ,protectd modifier.
If you dont want to access the members of a class then according to you requrement make that class members protected,private ..
Anchit Herredia
Greenhorn

Joined: Jul 15, 2009
Posts: 26
Stephan van Hulst wrote:
Anchit Here wrote:If you have a public class you can go about using it wherever you want which is very simple.


I would like to go a little bit off topic here, and say that while public classes may be easy to use, they are hard to maintain. Strive to only make classes public if they really have to be. Always prefer default access.


Thanks for pointing this out. I wasn't trying to promote using public classes. I should have said "which is a very simple concept" to make it more clear (because I think beginners would probably find public modifier to be the simplest to learn). Btw, why do you prefer default access more?
suhaib mustafa
Greenhorn

Joined: Apr 01, 2012
Posts: 10
I will try to answer this question in simple terms.

As you know default is for package level access and protected is for package level plus non-package classes but which extends this class(Point to be noted here is you can extend the class only if it is visible!).
lets put it in this way:

- protected top-level class would be visible to classes in its package.
- now making it visible outside the package (subclasses ) is bit confusing and tricky. Which classes should be allowed to inherit our protected class?
- If all the classes are allowed to subclass then it will be similar to public access specifier.
- If none then it is similar to Default.

Since there is no way to restrict this class being subclassed by only few classes ( we cannot restrict class being inherited by only few classes out of all the available classes in a package/outsite of a package or do we??!!), there is no use of protected access specifiers for top level classes. Hence it is not allowed.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Even more simpler, "protected" access modifier is for only protecting the inner class, properties(instance variables) and behaviors(methods) of Class...

"protected" entity is a confidential entity which can be accessed by consumer but have to follow the condition.

Consumer following the condition will be given access to protected entity... Condition is simply "follow the rules" i.e., in terms of OOPS,

inherit the class then the sub-class will be enabled to access the protected entities.


SCJP 6 [SCJP - Old is Gold]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Class cannot be protected (or) static ?
 
Similar Threads
Can u clarify this ..
protected class ???
protected class
Top level class??