Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package vs inheritance

 
cata lin
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Congratulations maha anna for the exam.
I played a little the roundup game. At question marked C9 #209 I was asked :
For object X to access a method of object Y, when the method has no access modifier, , object X MUST be an instance of class which is
a: in the same package as Y
b: declared public
c: a subclass of Y
d: the same class as Y
The answer was indicated as a, while I choosed c.
Why am I wrong , please ?
Many thanks.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class/method has no access modifier, it is said to have "package" access. ie., only accessible from the classes/methods in the same package. The default modifier is more restrictive than "protected" which provides subclass access.
Hope this helps. If not, try to write a small program by yourself and verify my statements.
Good luck.

Ajith
 
maha anna
Ranch Hand
Posts: 1467
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajith,
A small correction in your statement.
'The default modifier is more restrictive than "protected" which provides subclass access which are in other packages too.. The other packages is VERY important. because ONLY subclasses in other packages get this previlege NOT non-subclasses in other packages.
cata lin,
This is the concept:
package (no access modifier) - means that class/that method/that variable is seen all the fellow classes in the SAME PACAKGE ONLY. And also if and only if a class is in the SAME pacakge. The fellow class may be anything. It may be a subclass/may not be a subclass. All in the same pack can use this no-access-modifier class/ no-access-modifier method/
no-access-modifier var.
Also note this. Even if is a subclass , if it fails to be inside the SAME package , which means ,if it is in another package, then the subclass is treated as an outsider.
It is like saying, eventhough you are a member of my family (via class inheritance), since you are NOT frindlier to us (not in the same pack , then you are NOT invited here Being a good friend is MORE IMPORTANT than just being a child of the familiy alone like that.
So coming back specific to your doubt, Since the qstn specifically says the method has a default access, in order to get the previlege of using this method, the other class of object X HAS TO BE IN THE SAME PACAKGE. , If not you are treated as an alien. Clear ?
regds
maha anna
<pre>
class Xclass {
void xMethod() {
Yclass yObject = new Yclass();
yObject.yMethod();
}
}
class Yclass {
void yMethod() {}
}
</pre>

[This message has been edited by maha anna (edited May 11, 2000).]
 
cata lin
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the clarification. I somehow always miss that sugaring on the cake
Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic