aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected class?" Watch "protected class?" New topic
Author

protected class?

krishna amit
Greenhorn

Joined: Dec 12, 2000
Posts: 5
can anyone explain why we cannot write a class as protected?
ali farid
Ranch Hand

Joined: Aug 26, 2000
Posts: 60
can anyone explain why we should write a class as protected
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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).]
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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.
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
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.--.


Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
I am trying to compile raj's code in JDK1.3 it is not generating class file.


I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
Harish Babu.N
Greenhorn

Joined: Oct 12, 2000
Posts: 11


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.--.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected class?
 
Similar Threads
Question on protected access modifier
protected member
WA #1.....word association
protected class
private and protected class - why not?