• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected class?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain why we cannot write a class as protected?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain why we should write a class as protected
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.--.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to compile raj's code in JDK1.3 it is not generating class file.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.--.


 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic