Forums Register Login

protected class?

+Pie Number of slices to send: Send
can anyone explain why we cannot write a class as protected?
+Pie Number of slices to send: Send
can anyone explain why we should write a class as protected
+Pie Number of slices to send: Send
Here's an attempt at your questions: First if a class could be declared protected(which it can't of course) this would mean that it could not be inherited from another class in another package because the only members you can have access to in another package are the public ones, and since the class is protected, you can't access any of the members. This means that in essence, it is more restrictive and acts the same as the default "package access" where only members of the same package can access its members. I hope that helps. Someone correct me if I'm wrong, but I think I'm right.
[This message has been edited by Sean Casey (edited December 23, 2000).]
+Pie Number of slices to send: Send
I've found an exception to the rule. An inner class can be private or protected, but then again an inner class needs an outer class.
+Pie Number of slices to send: Send
Helo Krishna ,

can anyone explain why we cannot write a class as protected?
Who say that we can not write a class as protected. "Java is a funny Language as English."
Have you ever worked with a source code that fails to compile but when you try to run it works just fine. Well this
thing just happen in Java. Check the following code.
protected class Test {
public void SomeMethod (){
System.out.println("Checking if it is working with protected");
}
public static void main (String args[]){
Test obj = new Test();
obj.SomeMethod();
}
}
when you try to compile it will give you compile error saying somthing like class or interface expected.
But on the other hand when you will check you'r directory where you have stored the Test.java file you will see
Test.class file. And now try to run this Test file. It will give you desired output. And this thing will
work with private, static and any combination of access modifiers.
But as the API says that top level class can only be either public or friendly ("default"). And this is the bottom line
Regards,
Raj.--.
+Pie Number of slices to send: Send
I am trying to compile raj's code in JDK1.3 it is not generating class file.
+Pie Number of slices to send: Send


Hi rajpal
It gave me compiler error but class file was created and i was able to run the program .Can u explain why is this?
Regards
Harish


Originally posted by Rajpal Kandhari:
Helo Krishna ,

can anyone explain why we cannot write a class as protected?
Who say that we can not write a class as protected. "Java is a funny Language as English."
Have you ever worked with a source code that fails to compile but when you try to run it works just fine. Well this
thing just happen in Java. Check the following code.
protected class Test {
public void SomeMethod (){
System.out.println("Checking if it is working with protected");
}
public static void main (String args[]){
Test obj = new Test();
obj.SomeMethod();
}
}
when you try to compile it will give you compile error saying somthing like class or interface expected.
But on the other hand when you will check you'r directory where you have stored the Test.java file you will see
Test.class file. And now try to run this Test file. It will give you desired output. And this thing will
work with private, static and any combination of access modifiers.
But as the API says that top level class can only be either public or friendly ("default"). And this is the bottom line
Regards,
Raj.--.


Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 920 times.
Similar Threads
Why cannot an abstract class be instantiated
protected class ???
protected class
static variable
Question on protected access modifier
More...

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