Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between public and private

 
Amol Dongare
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I want to know the difference between public and private.
Thanks in advance.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has nothing to do with servlets, but in short

private means only visible within the class in which a method of variable is defined.

public means visible for everyone who knows how to find your method, class of variable.
 
npk chowdary
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to that private members cannot be inherited.so they are not able to override.but it seems like overriding.check for it.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the members private are of class and os members with public are access by all.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by camilo lopes:
the members private are of class and os members with public are access by all.


What are those "os" members here?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The data which you keep within yourself without revealing outside, is private. Say for example your "salary"

Whereas some information you may reveal without losing anything. for example, your name.

How do you have a control on its visibility? There are certain access modifiers to achieve the same. "private" and "public" are two among those and they help us in providing the control over the variables which represent the data we are interested in securing it.

Private - visible only to the class where it is present. same as your salary is only visible /known to you.

Public - visible to all public. Same as your name, Date of Birth etc.

Does that help?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Amol Dongare
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic