• Post Reply Bookmark Topic Watch Topic
  • New Topic

can a protected class be inherited by other class within or outside the package?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, protected members of the class (variables, methods) can be accessed by classes within the package as well as its subclasses.

But what about if I make the class protected?
Will java allow to be inherited by other classes outside the package?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:But what about if I make the class protected?


What happened when you tried to use the protected access level on a class?
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it.

1. If 2 classes on a same source code file: One protected and the other isn't, compilation error is not generated
2. If 2 classes diff source code file same package, compilation error occured
3. If 2 classes diff package, compilation error still occurred.

Does it mean having a protected class is exclusive only for the source code file?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java Language Specification you should not get even no 1 to compile. Was the protected class a member class? That is the only way you could get it to compile.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, they only are in the same source code file:

TestClass.java:

protected class TestClass {

}

class TryToInherit extends TestClass{

}

it compiled successfully.

Other than that, all else fails
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not when I tried, it didn't:-It must have been a member class, or you are using a compiler which does not conform to the JLS.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The IDE I am using JDEVELOPER.

Can you please tell me how can I determine if I am following the JLS?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why everyone will tell you that you shouldn't attempt to learn java using IDE.
Try to write your programs using some text editor (I like Notepad++).
Compile and run them from command line.
That way wou'll learn how Java works.

To find out the java version on your system try to run java -version and javac -version from command line. Note that your IDE might be using some other compiler/JDK.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:As far as I know, protected members of the class (variables, methods) can be accessed by classes within the package as well as its subclasses.

But what about if I make the class protected?
Will java allow to be inherited by other classes outside the package?


Since (as already mentioned) protected top-level classes don't exist -- that part of the question is moot. Or more correctly, the answer is "no". You can't inherit from them, because having them will mean your program won't compile.


To answer the question regarding protected class members of the class -- meaning protected inner classes -- the answer is "yes". You can inherit from those classes by classes outside of the package. In fact, once it is accessible, you can practically do anything with them, as you would a top-level class.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!