Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access on Java classes

 
Sudhakar Reddy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic