why a class can't access protected method from another class in thesame package,the method is interi

Discussion in 'Java' started by junzhang1983@gmail.com, Jan 26, 2008.

  1. Guest

    package XX;
    class A
    {
    protected void kk ();
    }

    package YY;
    import XX.A;

    class B extends A
    {

    }

    package YY;
    class C
    {
    public void zz()
    {
    kk(); //why can call B.kk()????
    }
    }



    but if we modify as below:
    package YY;
    import XX.A;

    class B extends A
    {
    protected void kk ()
    {
    super.kk();
    }
    }


    why above modify can success?
    3ks
    , Jan 26, 2008
    #1
    1. Advertising

  2. Hal Rosser Guest

    Re: why a class can't access protected method from another class in the same package,the method is interited from the ohtner class from differnt package?

    <> wrote in message
    news:...
    > package XX;
    > class A
    > {
    > protected void kk ();
    > }
    >
    > package YY;
    > import XX.A;
    >
    > class B extends A
    > {
    >
    > }
    >
    > package YY;
    > class C
    > {
    > public void zz()
    > {
    > kk(); //why can call B.kk()????
    > }
    > }
    >

    _________
    Because protected keyword gives access to subclasses and other members of
    the same package.
    And class C is not a subclass and not in package XX.
    >
    >
    > but if we modify as below:
    > package YY;
    > import XX.A;
    >
    > class B extends A
    > {
    > protected void kk ()
    > {
    > super.kk();
    > }
    > }
    >
    >
    > why above modify can success?
    > 3ks

    Because B is a subclass of A
    Hal Rosser, Jan 26, 2008
    #2
    1. Advertising

  3. Lew Guest

    Re: why a class can't access protected method from another classin the same package,the method is interited from the ohtner class from differntpackage?

    <> wrote ...
    >> package XX;
    >> class A
    >> {
    >> protected void kk ();
    >> }
    >>
    >> package YY;
    >> import XX.A;
    >>
    >> class B extends A
    >> {
    >>
    >> }
    >>
    >> package YY;
    >> class C
    >> {
    >> public void zz()
    >> {
    >> kk(); //why can call B.kk()????
    >> }
    >> }


    This will not compile. Why does the comment refer to B.kk()? Class B is not
    involved in the C invocation at all. C does not have a method kk().

    --
    Lew
    Lew, Jan 26, 2008
    #3
  4. Roedy Green Guest

    Re: why a class can't access protected method from another class in the same package,the method is interited from the ohtner class from differnt package?

    On Fri, 25 Jan 2008 22:33:01 -0800 (PST),
    wrote, quoted or indirectly quoted someone who said :

    >why above modify can success?


    two things. Please use standard caps conventions when you write code,
    especially lower case package names. See
    http://mindprod.com/jgloss/codingconventions.html

    It makes your code look extremely weird to an eye accustomed to
    conformant code.

    Next read up on what the various scopes are supposed to do.

    See http://mindprod.com/jgloss/scope.html
    http://mindprod.com/jgloss/protected.html
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
    Roedy Green, Jan 28, 2008
    #4
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  2. Lambda
    Replies:
    2
    Views:
    381
    James Kanze
    Jul 16, 2008
  3. Thomas Watson Steen
    Replies:
    4
    Views:
    323
    Sisyphus
    Oct 4, 2004
  4. Peng Yu
    Replies:
    23
    Views:
    265
    Peter J. Holzer
    Jun 12, 2010
  5. John Reye
    Replies:
    28
    Views:
    1,331
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page