• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object ORiented Programming

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic