Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected class?

 
krishna amit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain why we cannot write a class as protected?
 
ali farid
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain why we should write a class as protected
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.--.
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compile raj's code in JDK1.3 it is not generating class file.
 
Harish Babu.N
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic