a small question about object-orientation

Discussion in 'Java' started by Tommy, Nov 22, 2004.

  1. Tommy

    Tommy Guest

    I have just started java-programming (Deitel), and have read about
    classes and member classes.
    Let's say I have a class Car, and this class has some private members
    and private functions. Any functions that is not a member of a car
    denies access, to those functions and variables in Car. Is it the
    compiler which controls this access control or what? How is this done?
     
    Tommy, Nov 22, 2004
    #1
    1. Advertising

  2. Tommy wrote:

    > I have just started java-programming (Deitel), and have read about
    > classes and member classes.
    > Let's say I have a class Car, and this class has some private members
    > and private functions.


    In Java, it's "methods", not "functions".

    > Any functions that is not a member of a car
    > denies access, to those functions and variables in Car. Is it the
    > compiler which controls this access control or what? How is this done?


    Both the compiler and the runtime system check visibility rules. However,
    this should not be thought of as "access control", since it can be
    circumvented via reflection, see the class java.lang.reflect.AccessibleObject.

    Visibility restrictions have nothing to do with security, they're about
    code organization. Public members form an API that other classes written
    by other people may rely on. By making a member private, the programmer
    says "this is internal stuff, it can change whenever I feel like it and
    is really none of your business".
     
    Michael Borgwardt, Nov 22, 2004
    #2
    1. Advertising

  3. Tommy wrote:

    > I have just started java-programming (Deitel), and have read about
    > classes and member classes.
    > Let's say I have a class Car, and this class has some private members
    > and private functions. Any functions that is not a member of a car
    > denies access, to those functions and variables in Car. Is it the
    > compiler which controls this access control or what? How is this done?


    You might want to start out next door on comp.lang.java.help, which is
    more suited to beginners. I have crossposted this response to that
    group, and directed followups there.

    Terminology: Java classes do not have functions, they have methods.

    Answer: The compiler checks access to class members at compile time, and
    the virtual machine checks again at run time. "How" is probably not a
    pertinent question, or at least not a sufficiently specific one.


    John Bollinger
     
    John C. Bollinger, Nov 22, 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. Mike Wahler
    Replies:
    1
    Views:
    381
    Az Tech
    Aug 2, 2003
  2. Nebur
    Replies:
    0
    Views:
    289
    Nebur
    Apr 4, 2006
  3. Vishal Naidu

    Object orientation.....

    Vishal Naidu, Sep 1, 2005, in forum: C Programming
    Replies:
    11
    Views:
    625
    Alexei A. Frounze
    Sep 28, 2005
  4. Tom Anderson
    Replies:
    17
    Views:
    491
    Tom Anderson
    Apr 1, 2011
  5. Krekna Mektek

    Object Orientation

    Krekna Mektek, Oct 5, 2005, in forum: Ruby
    Replies:
    15
    Views:
    255
    William James
    Oct 7, 2005
Loading...

Share This Page