This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ???
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... HTH.
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, Gabrielle P.S. If you don't declare private or public, package is the default!!