Question about Protected Modifier

Discussion in 'Java' started by AIK, Aug 18, 2004.

  1. AIK

    AIK Guest

    I have the following two classes:

    package birdpack;

    public class Bird {

    Bird() {}

    protected int nFeathers;

    }

    in bird.java

    and

    package duckpack;
    import birdpack.Bird;

    public class Duck3 extends Bird {

    Duck3() {}

    void foo() {
    Bird b = new Bird();
    int temp;
    temp = b.nFeathers;
    }

    }

    in Duck3.java

    When I compile Duck3 I get this error: nFeathers has protected access in
    birdpack.Bird. Why is this? I thought the point of a protected modifier is
    to allow children of a class to access parent's protected feature even if
    the child class is in a different package?
     
    AIK, Aug 18, 2004
    #1
    1. Advertising

  2. AIK

    Chris Smith Guest

    AIK wrote:
    > When I compile Duck3 I get this error: nFeathers has protected access in
    > birdpack.Bird. Why is this? I thought the point of a protected modifier is
    > to allow children of a class to access parent's protected feature even if
    > the child class is in a different package?


    Protected access allows access to members of the superclass of the same
    object, but not of a different object. Otherwise, protected members of
    java.lang.Object would be universally visible, which is obviously not
    the intent (and, of course, smaller-scale versions of the same would
    occur).

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 18, 2004
    #2
    1. Advertising

  3. AIK

    Rogan Dawes Guest

    AIK wrote:

    > I have the following two classes:
    >
    > package birdpack;
    >
    > public class Bird {
    >
    > Bird() {}
    >
    > protected int nFeathers;
    >
    > }
    >
    > in bird.java
    >
    > and
    >
    > package duckpack;
    > import birdpack.Bird;
    >
    > public class Duck3 extends Bird {
    >
    > Duck3() {}
    >
    > void foo() {
    > Bird b = new Bird();
    > int temp;
    > temp = b.nFeathers;
    > }
    >
    > }
    >
    > in Duck3.java
    >
    > When I compile Duck3 I get this error: nFeathers has protected access in
    > birdpack.Bird. Why is this? I thought the point of a protected modifier is
    > to allow children of a class to access parent's protected feature even if
    > the child class is in a different package?
    >
    >
    >


    Note that in the example above, you are not accessing the "protected
    int" of the parent class Bird, you are trying to access the "protected
    int" of the new Bird instance that you have instantiated.

    Something like:

    public class Duck3 extends Bird {

    Duck3() {}

    void foo() {
    System.out.println(nFeathers);
    }

    }

    in Duck3.java would work.

    Rogan
    --
    Rogan Dawes

    *ALL* messages to will be dropped, and added
    to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
     
    Rogan Dawes, Aug 18, 2004
    #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. Andreas Klemt
    Replies:
    2
    Views:
    582
    Andreas Klemt
    Jul 5, 2003
  2. Stephen

    Protected access modifier in Struct

    Stephen, Apr 6, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    407
    Stephen
    Apr 7, 2006
  3. Wenjie
    Replies:
    3
    Views:
    637
    Victor Bazarov
    Jul 21, 2003
  4. Matt Taylor

    Question on const pointer modifier

    Matt Taylor, Jan 21, 2004, in forum: C++
    Replies:
    4
    Views:
    384
    Victor Bazarov
    Jan 21, 2004
  5. Pradeep Kumar

    Protected access modifier working ???

    Pradeep Kumar, May 21, 2004, in forum: C++
    Replies:
    4
    Views:
    519
    David Harmon
    May 21, 2004
Loading...

Share This Page