Doubt regarding "Protected" access specifier across different packages

S

sayantan.chowdhury

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
 
A

Andrew Thompson

(e-mail address 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.aspx/java-general/200709/1
 
L

Lew

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

Andrew said:
The protected members are accessible, though not
quite in the way you are trying..
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.
 
A

Andrew Thompson

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.aspx/java-general/200709/1
 
A

Andrew Thompson

Lew wrote:
...
(Andrew T.)
<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.aspx/java-general/200709/1
 
L

Lew

Andrew said:
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.
 
S

sayantan

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
 
L

Lew

sayantan said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top