Doubt regarding "Protected" access specifier across different packages

Discussion in 'Java' started by sayantan.chowdhury@gmail.com, Sep 21, 2007.

  1. Guest

    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
     
    , Sep 21, 2007
    #1
    1. Advertising

  2. 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
     
    Andrew Thompson, Sep 21, 2007
    #2
    1. Advertising

  3. Lew Guest

    Re: Doubt regarding "Protected" access specifier across differentpackages

    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
     
    Lew, Sep 21, 2007
    #3
  4. 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
     
    Andrew Thompson, Sep 21, 2007
    #4
  5. 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.aspx/java-general/200709/1
     
    Andrew Thompson, Sep 21, 2007
    #5
  6. Lew Guest

    Re: Doubt regarding "Protected" access specifier across differentpackages

    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
     
    Lew, Sep 21, 2007
    #6
  7. Roedy Green Guest

    On Fri, 21 Sep 2007 05:34:47 -0700, ""
    <> 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
     
    Roedy Green, Sep 21, 2007
    #7
  8. sayantan Guest

    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
     
    sayantan, Sep 24, 2007
    #8
  9. Lew Guest

    Re: Doubt regarding "Protected" access specifier across differentpackages

    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
     
    Lew, Sep 24, 2007
    #9
  10. Roedy Green Guest

    On Mon, 24 Sep 2007 04:47:00 -0000, sayantan
    <> 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
     
    Roedy Green, Sep 25, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    569
    Andreas Klemt
    Jul 5, 2003
  2. Paul  Smith
    Replies:
    0
    Views:
    743
    Paul Smith
    Nov 18, 2003
  3. Replies:
    2
    Views:
    361
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Feb 7, 2007
  4. dost
    Replies:
    5
    Views:
    1,177
    Phlip
    Apr 28, 2006
  5. Davy
    Replies:
    2
    Views:
    423
    Terry Reedy
    Nov 10, 2007
Loading...

Share This Page