It's not a secret anymore!
The moose likes Other Languages and the fly likes Object ORiented Programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Object ORiented Programming" Watch "Object ORiented Programming" New topic

Object ORiented Programming

Ali Hassaan
Ranch Hand

Joined: May 16, 2001
Posts: 103
Hi all ranchers
I am developing for an year and half on Java.
Previously i was just developing but bow a days i have a responisbility to design also.
I have a question in my mind. First of all why we use private although we can use public.i.e let suppose we have two classes class A and Class B
if i want to share some members between A and B i just make them public or protected else members are private. So, if both the classes are in my use then whats problem if i just make them public i mean all members to be public. Is their any memory linkage between them or ???
Branka Kranjac

Joined: Mar 28, 2001
Posts: 7
Best solution is to make them package visible - all classes in same package will share variables, but none outside of package.
Difference is that if something is public, every class can access/modify them. If you want to avoid problems that this can cause (and lot of problems can arise from that approach in large project), just make them package variables - public for all classes in same package, invisible to all the rest...
Gabrielle Pieck

Joined: Feb 20, 2002
Posts: 3
Dear Ali,
If you are just programming for yourself, then it really doesn't matter. But, if you are programming for public consumption, you might make an analogy to a play at the theater. For the play to be a success, a lot has to happen back stage -- lighting, make-up, costume alteration, etc. The audience really isn't interested in these things, nor do you want them to see these things. It makes the experience of sitting in the audience much more pleasurable and the play much more convincing when "these objects and methods" are hidden. As the director of the play, you need to be able to manipulate everything on stage and backstage. But, as a member of the audience, you rather not see it all hang out as it were. The analogy isn't a perfect fit to public/private (ok, maybe a very far-fetched fit), but I hope it gives you more of an idea why it is important to distinguish between public and private.
All the best,
P.S. If you don't declare private or public, package is the default!!
I agree. Here's the link:
subject: Object ORiented Programming
It's not a secret anymore!