a small question about object-orientation

T

Tommy

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?
 
M

Michael Borgwardt

Tommy said:
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".
 
J

John C. Bollinger

Tommy said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top