Forums Register Login

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
The IDE I am using JDEVELOPER.

Can you please tell me how can I determine if I am following the JLS?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 855 times.
Similar Threads
Kathy/Bert - great explanation of protected members
Confused with protected access
Protected Scope
protected can not be accessed in derived ??
Section 6.6.7 of JLS ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:44:49.