aspose file tools*
The moose likes Beginning Java and the fly likes Access on Java classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access on Java classes" Watch "Access on Java classes" New topic
Author

Access on Java classes

Sudhakar Reddy
Greenhorn

Joined: Dec 28, 2000
Posts: 4
I know java the following syntax for class declaration
public class MyCl {
private int getNum() { return 5; }
}
And also
class MyCl {
private int getNum() { return 5; }
}
A. Is there any difference between these two declarations ?
B. Can I use other access permissions private and protected in the class declaration
for eg: protected class MyCl {
...
}

private class MyCl {
...
}
Thank you
Sudhakar
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
class MyClass{ }
protected class MyClass { }
these are actually the same ... when you do not provide an access modifer to a class it is assumed to be protected.
You can also use Private as an access modifier. Be careful though if you are calling methods in this class from another class!


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
Angela,
I think you are wrong. Deafult(no access modifier) mean the class is accesseble with in the package, not protected. Protected mean class in different packages can extend this class which is not true for DEFAULT accessibility.
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
Angela,
I think you are wrong. Deafult(no access modifier) mean the class is accesseble with in the package, not protected. Protected mean class in different packages can extend this class which is not true for DEFAULT accessibility.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Angela,
I don't think that the two class declarations are the same.
class MyClass with no modifier is assumed to be "friendly" which means any other class in the same package can use this class, and NO ONE else.
protected class MyClass means any other class in the same package AND any subclass of MyClass, even if it resides in a different package, can use it. This is less restrictive. If you are in an environment with lots of java applications, and want to share code this can be a big difference.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
OK my bad .... you're right! I actually never knew that but just checked. Well it's back to the books for me ... it always worries me when I get something basic wrong. Apologies to anyone I confussed!
That'll teach me to check next time I'm answering something! Thanks guys
Sajan Joseph
Ranch Hand

Joined: Jan 05, 2001
Posts: 40
Hey ,
I was under the impression that you cannot apply any other access modifier other that public or 'default' to a top level class declaration though an inner class can have any access modifier.
But the posted messages suggest otherwise. Can anyone please clarify?
Thanks,
SJ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access on Java classes
 
Similar Threads
Write a class called GenNumbers to generate the following output ?
how is this answear correct?
overriding / overloading
K & B 1.5 Chapter 8 self Test Q.12
Overriding Instance Variables (help)