Private member

Discussion in 'Java' started by sujee, Jan 21, 2005.

  1. sujee

    sujee Guest

    hi i have a problem. pls help.


    class A {
    private int i;

    void seti(int x) {
    i = x;
    }

    int geti(){
    return i;
    }
    }


    class B extends A {
    }


    class PrivateMember {
    public static void main(String args[]) {
    B subOb = new B();

    subOb.seti(10);
    System.out.println(subOb.geti());
    }
    }



    In the above program the super class 'A' have an private member 'i'.
    since it is a private member, 'i' is not a member of sub class 'B'. and
    in the super class it have 2 methods seti() and geti() to set and get
    the 'i'. In the 3rd class 'PrivateMember', it create an subclass
    instance and run both methods. since 'i' is not an member of subclass,
    how the subclass instance set and get 'i'. when creating an instance it
    allocates the memory locations for its member. but 'i' is not a member.
    where is the location of the 'i' that is set by seti method? pls help
     
    sujee, Jan 21, 2005
    #1
    1. Advertising

  2. On Fri, 21 Jan 2005 03:56:51 -0800, sujee wrote:

    > [...] since 'i' is not an member of subclass [...]


    i is a member of the subclass. It is just not _visible_ to the subclass.
    --
    In pioneer days they used oxen for heavy pulling, and when one ox
    couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
    be trying for bigger computers, but for more systems of computers.
    --- Rear Admiral Grace Murray Hopper
     
    Stefan Schulz, Jan 21, 2005
    #2
    1. Advertising

  3. sujee

    Guest

    Try declaring i and the methods in A protected.
     
    , Jan 21, 2005
    #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. qazmlp
    Replies:
    19
    Views:
    797
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    428
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    514
    Steven Bethard
    Jan 24, 2005
  4. Peng Yu
    Replies:
    3
    Views:
    1,084
    Simon Forman
    Sep 21, 2009
  5. ittium
    Replies:
    5
    Views:
    443
    88888 Dihedral
    Jan 12, 2012
Loading...

Share This Page