child access to parent class protected member

Discussion in 'Java' started by cy, Apr 13, 2007.

  1. cy

    cy Guest

    My question involves child class access to protected member of parent
    class. (Arose from attempting to solve exercise in Bruce Eckel's
    book).
    The following three files, each in its own separate packages, compile
    and run just fine:
    /* // in separate package:
    * public interface Ex6Interface {
    * String say();
    * }
    *
    * // and in a second package:
    * public class Ex6Base {
    * protected class Ex6BaseInner implements Ex6Interface {
    * public String say() { return "Hi"; }
    * }
    * public Ex6BaseInner getEx6BaseInner() {
    * return new Ex6BaseInner();
    * }
    * }
    */
    import innerclasses.ex6Interface.*;
    import innerclasses.ex6Base.*;

    public class Ex6 extends Ex6Base {
    Ex6Interface getBaseInner() {
    // Error: Ex6BaseInner has protected access:
    // return this.new Ex6BaseInner();
    // So, to create protected class use public method:
    return this.getEx6BaseInner();
    }
    public static void main(String[] args) {
    Ex6 ex = new Ex6();
    System.out.println(ex.getBaseInner().say());
    }
    }

    -----------------
    I left in my comments in the getBaseInner() method, to show error I
    got with line: return this.new ExBaseInner();
    which I thought should work.
    Why do we neet a public method to access protected class member
    Ex6BaseInner of parent Ex6Base? Don't subclasses have access to
    protected member of parent class?
    Greg
     
    cy, Apr 13, 2007
    #1
    1. Advertising

  2. cy

    Filip Larsen Guest

    cy wrote:

    > My question involves child class access to protected member of parent
    > class.


    > Why do we neet a public method to access protected class member
    > Ex6BaseInner of parent Ex6Base? Don't subclasses have access to
    > protected member of parent class?


    The line "return new Ex6BaseInner();" relies on an accessible
    constructor, but there are none because the default constructor of
    Ex6BaseInner is protected due to Ex6BaseInner being a protected class.
    See section 8.8.9 in the JLS [1] for details.

    You example will work if you just add a public constructor to Ex6BaseInner.


    [1]
    http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Apr 13, 2007
    #2
    1. Advertising

  3. cy

    Piotr Kobzda Guest

    Filip Larsen wrote:
    > cy wrote:
    >
    >> My question involves child class access to protected member of parent
    >> class.

    >
    >> Why do we neet a public method to access protected class member
    >> Ex6BaseInner of parent Ex6Base? Don't subclasses have access to
    >> protected member of parent class?

    >
    > The line "return new Ex6BaseInner();" relies on an accessible
    > constructor, but there are none because the default constructor of
    > Ex6BaseInner is protected due to Ex6BaseInner being a protected class.
    > See section 8.8.9 in the JLS [1] for details.
    >
    > You example will work if you just add a public constructor to Ex6BaseInner.


    Another solution, not requiring publicly accessible constructor in
    Ex6BaseInner, might be introducing another inner class in the Ex6 which
    extends inherited Ex6BaseInner class. In particular, that inner class
    may be anonymous, so the following should work as well:

    return this.new Ex6BaseInner() { };


    piotr
     
    Piotr Kobzda, Apr 13, 2007
    #3
    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. Grey Plastic
    Replies:
    4
    Views:
    842
    pw4getter
    Mar 5, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    837
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    0
    Views:
    227
  4. Noel Dolan
    Replies:
    0
    Views:
    274
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    182
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page