Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Doubt regarding "Protected" access specifier across different packages

Thread view: 
sayantan.chowdhury@gmail.com - 21 Sep 2007 13:34 GMT
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
Andrew Thompson - 21 Sep 2007 14:19 GMT
(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.

Signature

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

Lew - 21 Sep 2007 14:29 GMT
sayantan.chowdhury@gmail.com 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.

> 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{
>
[quoted text clipped - 13 lines]
> 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.

Signature

Lew

Andrew Thompson - 21 Sep 2007 14:40 GMT
...
(Andrew T.)
>> ..going to avoid trying to explain further, I'll leave that
>> to the JLS/OO gurus.
[quoted text clipped - 3 lines]
>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"?   ;-)

Signature

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

Lew - 21 Sep 2007 14:44 GMT
>>> ..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.

Signature

Lew

Andrew Thompson - 21 Sep 2007 14:34 GMT
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().

Signature

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

Roedy Green - 21 Sep 2007 23:44 GMT
On Fri, 21 Sep 2007 05:34:47 -0700, "sayantan.chowdhury@gmail.com"
<sayantan.chowdhury@gmail.com> 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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

sayantan - 24 Sep 2007 05:47 GMT
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
[quoted text clipped - 5 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Lew - 24 Sep 2007 06:20 GMT
> 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?

Signature

Lew

Roedy Green - 25 Sep 2007 03:58 GMT
On Mon, 24 Sep 2007 04:47:00 -0000, sayantan
<sayantan.chowdhury@gmail.com> 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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.