Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Doubt regarding "Protected" access specifier across different packages

Reply
Thread Tools

Doubt regarding "Protected" access specifier across different packages

 
 
sayantan.chowdhury@gmail.com
Guest
Posts: n/a
 
      09-21-2007
Hi all,

I have the following piece of code -

************************************************** *****************************************
1. package MyPack;
2.
3. public class Balance {
4. private String name;
5. private double bal;
6.
7. protected Balance() {
8.
9. }
10.
11. protected Balance (String A,double B) {
12. name = A;
13. bal = B;
14. }
15.
16. protected void Show () {
17.
18. System.out.println(name +" : $"+bal);
19. }
20. } /*class Balance*/
21.
22. // Subclass of Balance in a different package
23.
24. package testing;
25.
26. import MyPack.Balance;
27.
28. public class TestBalance extends Balance{
29.
30. public static void main(String args[]) {
31.
32. Balance mybalance = new Balance("Someone",1000);
33. mybalance.Show();
34.
35. }
36. } /*class TestBalance*/

************************************************** ************************************************** ***

While compiling the above code, I'm getting the following error in
line #32 -
"The constructor Balance(String,double) is not visible"

and the following error in line #33 -
"The method Show() from the type Balance is not visible"

Could someone please explain to me the reasoning behind these errors.
As I understand, protected members are accesible to the subclasses in
a different package.

Any pointers to any reading material will be appreciated.

Thanks,
Sayantan Chowdhury

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

(moved from below - to better suit my reply.)
>Could someone please explain to me the reasoning behind these errors.
>As I understand, protected members are accesible to the subclasses in
>a different package.


The protected members are accessible, though not
quite in the way you are trying..

See my changes to your second class.

>I have the following piece of code -
>
>************************************************* ******************************************
>1. package MyPack;
>2.
>3. public class Balance {

...
>20. } /*class Balance*/
>21.
>22. // Subclass of Balance in a different package

package testing;

import MyPack.Balance;

public class TestBalance extends Balance{

TestBalance(String name, double balance) {
super(name, balance);
}

public static void main(String args[]) {

TestBalance mybalance = new TestBalance("Someone",1000);
mybalance.Show();

}
} /*class TestBalance*/

The code as above should compile cleanly. I am
going to avoid trying to explain further, I'll leave that
to the JLS/OO gurus.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200709/1

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-21-2007
(E-Mail Removed) wrote:
>> Could someone please explain to me the reasoning behind these errors.
>> As I understand, protected members are accesible to the subclasses in
>> a different package.


For use for their own construction only, not to create "third-party" objects.

Andrew Thompson wrote:
> The protected members are accessible, though not
> quite in the way you are trying..


>> 22. // Subclass of Balance in a different package

> package testing;
>
> import MyPack.Balance;


BTW, OP, packages should be named with all lower-case letters, by convention.

> public class TestBalance extends Balance{
>
> TestBalance(String name, double balance) {
> super(name, balance);
> }
>
> public static void main(String args[]) {
>
> TestBalance mybalance = new TestBalance("Someone",1000);
> mybalance.Show();
>
> }
> } /*class TestBalance*/
>
> The code as above should compile cleanly. I am
> going to avoid trying to explain further, I'll leave that
> to the JLS/OO gurus.


<http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2.2>

You can use a constructor for the object itself, inheriting the constructor,
but not for another object, where inheritance isn't involved.

--
Lew
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-21-2007
But a couple of small points I forgot to mention.

Using the common nomenclature for classes, methods
and attributes helps others to understand the code.

These common 'rules'* would mean that MyPack
should be all lower case, hence mypack, but even
then, abbreviations within the lower case form can
become very confusing, so 'mypackage' would be
better.

Now that I say that, I realise that the majority (not all)
of package names in the J2SE are one word.

On the same topic, methods and non-final attributes
should be what is known as camelCase, with first
letter lower case, and each other word starting with
an Upper Case letter. So, e.g. Show() should be show().

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200709/1

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-21-2007
Lew wrote:
...
(Andrew T.)
>> ..going to avoid trying to explain further, I'll leave that
>> to the JLS/OO gurus.

>
><http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2.2>
>
>You can use a constructor for the object itself, inheriting the constructor,
>but not for another object, where inheritance isn't involved.


(Checks watch) 10 minutes (swears loudly).
Does that mean I don't "get my pizza for free"?

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200709/1

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-21-2007
Andrew Thompson wrote:
>>> ..going to avoid trying to explain further, I'll leave that
>>> to the JLS/OO gurus.


Lew wrote:
>> <http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2.2>
>>
>> You can use a constructor for the object itself, inheriting the constructor,
>> but not for another object, where inheritance isn't involved.


AT:
> (Checks watch) 10 minutes (swears loudly).
> Does that mean I don't "get my pizza for free"?


I hadn't actually known that rule until this question. I intuited it and
smiled when I saw your answer, because it meant I was right, but I didn't know
it until I looked it up.

Whenever faced with a subtle or bizarre Java behavior, I hit the JLS. It's
almost always settled the matter within seconds.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-21-2007
On Fri, 21 Sep 2007 05:34:47 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Could someone please explain to me the reasoning behind these errors.
>As I understand, protected members are accesible to the subclasses in
>a different package.


If you wan that extreme visibility, you must use public. Protected is
a bit weaker than that. See http://mindprod.com/jgloss/protected.html
http://mindprod.com/jgloss/scope.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
sayantan
Guest
Posts: n/a
 
      09-24-2007
Hi Roedy,

I went through the link you provided.It's written that the "protected"
is visible to classes outside the package that inherit the class.
And this is my exact point of confusion as I should not get these
errors accroding to this rule.

Thanks,
Sayantan

>
> >Could someone please explain to me the reasoning behind these errors.
> >As I understand, protected members are accesible to the subclasses in
> >a different package.

>
> If you wan that extreme visibility, you must use public. Protected is
> a bit weaker than that. Seehttp://mindprod.com/jgloss/protected.htmlhttp://mindprod.com/jgloss/scope.html
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com



 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-24-2007
sayantan wrote:
> Hi Roedy,
>
> I went through the link you provided.It's written that the "protected"
> is visible to classes outside the package that inherit the class.
> And this is my exact point of confusion as I should not get these
> errors accroding to this rule.


Please do not top-post.

This was answered earlier. Read:
<http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2.2>

As I mentioned:
> You can use a constructor for the object itself, inheriting the constructor,
> but not for another object, where inheritance isn't involved.


In other words, the protected constructor can only be used by the object
itself, i.e., through an implicit or explicit super() call (or other
constructor). You were calling the constructor of an object other than this.
That is forbidden.

It is not enough that the invoking object be of the child class. It must also
be constructing itself.

Did you have difficulty with this answer before? What is the part that gives
you trouble?

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-25-2007
On Mon, 24 Sep 2007 04:47:00 -0000, sayantan
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>I went through the link you provided.It's written that the "protected"
>is visible to classes outside the package that inherit the class.
>And this is my exact point of confusion as I should not get these
>errors accroding to this rule.


Oops, I gave you the wrong URL. Try this improved one.

http://mindprod.com/jgloss/protectedscope.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to link different site-packages to different Python? Davy Python 2 11-10-2007 06:19 AM
doubt regarding conversion specifier in case of printfs,sprintfs... ranjiththarayil@gmail.com C Programming 2 02-07-2007 02:18 PM
diffrence between access modifier ....and access specifier...in c++ dost C++ 5 04-28-2006 01:40 AM
Do the javax.imageio packages replace packages in com.sun.image? Paul Smith Java 0 11-18-2003 02:58 PM



Advertisments